getChars()

If you need to extract more than one character at a time, you can use the getChars( ) method.

It has this general form:

void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart)
  • sourceStart specifies the index of the beginning of the substring
  • sourceEnd specifies an index that is one past the end of the desired substring.

The substring contains the characters from sourceStart through sourceEnd-1. The array that will receive the characters is specified by target. The index within target at which the substring will be copied is passed in targetStart.

To extract more than one character, use the getChars( ) method.

The following program demonstrates getChars( ). It gets a sub set of the chars.

 
public class Main {
  public static void main(String args[]) {

    String s = "This is a test string from java2s.com.";
    int start = 10;
    int end = 14;
    char buf[] = new char[end - start];

    s.getChars(start, end, buf, 0);
    System.out.println(buf);
  }
}

Here is the output of this program:


test
Home 
  Java Book 
    Essential Classes  

String:
  1. String type and Literals
  2. String Concatenation
  3. String.CASE_INSENSITIVE_ORDER
  4. String Constructor
  5. charAt(int index):Get a single char by index
  6. String: compareTo(String stringValue)
  7. concat(String str)
  8. equals():Compare two string value for equality
  9. equals( ) vs ==
  10. contains(CharSequence s)
  11. copyValueOf(char[] data)
  12. endsWith(String suffix)
  13. format():Format a string
  14. getBytes():Get byte array from string
  15. getChars()
  16. indexOf
  17. intern a string
  18. isEmpty:if string is empty
  19. lastIndexOf()
  20. length() Returns the length of this string
  21. startsWith( )
  22. toLowerCase() and toUpperCase(): convert string case with locale
  23. substring:Get sub string from a string
  24. toCharArray():Get char array from string
  25. toString( )
  26. trim()
  27. valueOf():Convert boolean, char, double, float,int,long,object to String