例如6:
package Chapter06.string;
public class StringDemo_06 {
public static void main(String[] args) throws Exception {
// ISO8859-1:拉丁文
System.out.println("ISO8859-1字符集与gb2312之间的转换:");
String str = new String("大家好".getBytes("gb2312"), "ISO8859-1"); // 将gb2312字符集转码为ISO8859-1字符集
String str1 = new String(str.getBytes("ISO8859-1"), "gb2312"); // 将ISO8859-1字符集转码为gb2312字符集
System.out.println("ISO8859-1字符集:" + str + "\ngb2312字符集:" + str1);
// KOI8:俄文
System.out.println("\nKOI8字符集与gb2312之间的转换:");
String str2 = new String("你好".getBytes("gb2312"), "KOI8"); // 将gb2312字符集转码为KOI8字符集
String str3 = new String(str2.getBytes("KOI8"), "gb2312"); // 将KOI8字符集转码为gb2312字符集
System.out.println("KOI8字符集:" + str2 + "\ngb2312字符集:" + str3);
// Big5:繁体中文
System.out.println("\nBig5字符集与gb2312之间的转换:");
String str4 = new String("谢谢你".getBytes("gb2312"), "Big5"); // 将gb2312字符集转码为Big5字符集
String str5 = new String(str4.getBytes("Big5"), "gb2312"); // 将Big5字符集转码为gb2312字符集
System.out.println("Big5字符集:" + str4 + "\ngb2312字符集:" + str5);
// ISO8859-5:西里尔文
System.out.println("\nISO8859-5字符集与gb2312之间的转换:");
String str6 = new String("美丽的中国".getBytes("gb2312"), "ISO8859-5"); // 将gb2312字符集转码为ISO8859-5字符集
String str7 = new String(str6.getBytes("ISO8859-5"), "gb2312"); // 将ISO8859-5字符集转码为gb2312字符集
System.out.println("ISO8859-5字符集:" + str6 + "\ngb2312字符集:" + str7);
}
}
例如7:
package Chapter06.string;
public class StringDemo_07 {
public static void main(String[] args) {
System.out.println("String类的indexOf方法的使用示例如下:");
String str = "Looking for the first time in the specified character position";
int a = str.indexOf("f".codePointAt(0)); // 返回第一次出现指定字符的Unicode代码处的索引
int b = str.indexOf("a"); // 返回字符a第一次出现的位置
int c = str.indexOf("p".codePointAt(0), 3); // 返回从指定的索引开始搜索,第一次出现指定字符的Unicode代码的索引
int d = str.indexOf("m", 5); // 返回指定字符从指定的索引处开始,第一次出现的索引
System.out.println("a = " + a + " str.charAt(a)=" + str.charAt(a));
System.out.println("b = " + b + " str.charAt(b)=" + str.charAt(b));
System.out.println("c = " + c + " str.charAt(c)=" + str.charAt(c));
System.out.println("d = " + d + " str.charAt(d)=" + str.charAt(d));
}
}
例如8:
package Chapter06.string;
public class StringDemo_08 {
public static void main(String[] args) {
System.out.println("String类的lastIndexOf方法的使用示例如下:");
String str = "Characters to find the specified location last seen";
int a = str.lastIndexOf("d".codePointAt(0)); // 返回最后一次出现指定字符的Unicode代码处的索引
int b = str.lastIndexOf("a"); // 返回字符a最后一次出现的位置
int c = str.lastIndexOf("i".codePointAt(0), 23);// 返回从指定的索引向前开始搜索,最后一次出现指定字符的Unicode代码的索引
int d = str.lastIndexOf("e", 25);// 返回指定字符从指定的索引处从右向左开始搜索,最后一次出现的索引
System.out.println("a = " + a + " str.charAt(a)=" + str.charAt(a));
System.out.println("b = " + b + " str.charAt(b)=" + str.charAt(b));
System.out.println("c = " + c + " str.charAt(c)=" + str.charAt(c));
System.out.println("d = " + d + " str.charAt(d)=" + str.charAt(d));
}
}
例如9:
package Chapter06.string;
public class StringDemo_09 {
public static void main(String[] args) {
System.out.println("String类的length方法的使用示例如下:");
String str = "kdflsakflsdfjsahdksl;fjsksdlkf;fl";
int len = str.length();
System.out.println("字符串:\n" + str + "的长度为:" + len);
}
}
例如10:
package Chapter06.string;
public class StringDemo_10 {
public static void main(String[] args) {
String str = "我很欣赏你,因为你是个好人";
String newStr = str.replace('你', '他'); // 调用replace方法,将字符串str中的字符'你'换成'他'
System.out.println("替换前的字符串的内容如下:\n " + str);
System.out.println("\n替换后的字符串的内容如下:\n " + newStr);
}
}