Java String and char array
In this chapter you will learn:
- How to Get a single char by index
- How to copy part of a char array to String
- How to Get char array from string
- How to get char or char array from a string
Get a single char by index
To extract a single character from a String, you can refer directly to an individual character
via the charAt()
method.
char charAt(int index)
returns the char value at the specified index.
public class Main {
public static void main(String[] argv) {
byte[] bytes = new byte[]{67,68,69};
// j a v a2s . co m
String str = new String(bytes);
System.out.println(str.charAt(0));
}
}
The output:
Copy part of a char array to String
We can create a String value by copying value from a char array.
static String copyValueOf(char[] data)
returns a String that represents the character sequence in the array specified.static String copyValueOf(char[] data, int offset, int count)
returns a String that represents the character sequence in the array specified.
public class Main {
public static void main(String[] argv) {
char[] chars = new char[] { 'd', 'e', 'm', 'o', '2', 's', '.', 'c', 'o','m' };
/* jav a 2 s .co m*/
System.out.println(String.copyValueOf(chars));
}
}
The output:
copyValueOf(char[] data, int offset, int count)
copies value from a char array with offset.
public class Main {
/*j ava 2s . c om*/
public static void main(String[] arg) {
char[] textArray = { 'T', 'o', ' ', 'b', 'e', ' ', 'o', 'r', ' ', 'n', 'o', 't', ' ', 't', 'o',
' ', 'b', 'e' };
String text = String.copyValueOf(textArray, 9, 3);
System.out.println(text);
}
}
Get char array from string
char[] toCharArray()
converts this string to a new character array.
import java.util.Arrays;
//j ava2s .com
public class Main {
public static void main(String[] argv) {
String str = "java2s.com";
char[] chars = str.toCharArray();
System.out.println(Arrays.toString(chars));
}
}
The output:
Get char array out of a sub string
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 substringsourceEnd
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[]) {
//from j a va 2 s . c o m
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:
Next chapter...
What you will learn in the next chapter:
- How to Convert boolean, char, double, float,int,long,object to String
- How to Convert string case with locale
- How to encode a string into byte array