public class StringTest {
public static void main(String[] args) {
String str1 = "ABCDEFGHIJ1234567890";
String str2 = "ABCDEFGHIJ1234567890";
byte[] bstr1 = str1.getBytes();
byte[] bstr2 = str2.getBytes();
byte[] butf8str1 = null;
byte[] butf8str2 = null;
byte[] butf16str1 = null;
byte[] butf16str2 = null;
byte[] buastr1 = null;
byte[] buastr2 = null;
try {
butf8str1 = str1.getBytes("UTF-8");
butf8str2 = str2.getBytes("UTF-8");
butf16str1 = str1.getBytes("UTF-16");
butf16str2 = str2.getBytes("UTF-16");
buastr1 = str1.getBytes("US-ASCII");
buastr2 = str2.getBytes("US-ASCII");
}
catch(Exception excp) {
}
System.out.println("str1 = [" + str1 + "]");
System.out.println("String.length : " + str1.length());
System.out.println("String.codePointCount : " + str1.codePointCount(0, str1.length()));
System.out.println("String.getBytes : " + bstr1.length);
System.out.println("String.getBytes(UTF-8) : " + butf8str1.length);
System.out.println("String.getBytes(UTF-16) : " + butf16str1.length);
System.out.println("String.getBytes(US-ASCII) : " + buastr1.length);
System.out.println("make func getLengh : " + getLength(str1));
System.out.println();
System.out.println("str1 = [" + str2 + "]");
System.out.println("String.length : " + str2.length());
System.out.println("String.codePointCount : " + str2.codePointCount(0, str2.length()));
System.out.println("String.getBytes : " + bstr2.length);
System.out.println("String.getBytes(UTF-8) : " + butf8str2.length);
System.out.println("String.getBytes(UTF-16) : " + butf16str2.length);
System.out.println("String.getBytes(US-ASCII) : " + buastr2.length);
System.out.println("make func getLengh : " + getLength(str2));
}
public static int getLength(String str) {
int length = 0;
for(int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if((ch >= 0x20) && (ch <= 0x7E)) {
length++;
}
else if((ch >= 0xFF61) && (ch <= 0xFF9F)) {
length++;
}
else {
length += 2;
}
}
return length;
}
}
str1 = [ABCDEFGHIJ1234567890]
String.length : 20
String.codePointCount : 20
String.getBytes : 20
String.getBytes(UTF-8) : 20
String.getBytes(UTF-16) : 42
String.getBytes(US-ASCII) : 20
make func getLengh : 20
str1 = [ABCDEFGHIJ1234567890]
String.length : 20
String.codePointCount : 20
String.getBytes : 60
String.getBytes(UTF-8) : 60
String.getBytes(UTF-16) : 42
String.getBytes(US-ASCII) : 20
make func getLengh : 40