?? stringdemo.java
字號:
public class StringDemo {
public static void main(String[] args) {
String str1 = "Hello world!This is Java code."; //聲明并初始化字符串str1
String str2; //聲明字符串str2
String str3 = new String("This is Java code."); //聲明并初始化字符串str3
str2 = new String("This is Java code."); //創建字符串對象str2并初始化
System.out.println("str1 is: "+str1);
System.out.println("str2 is: "+str2);
System.out.println("str3 is: "+str3+"\n");
System.out.println("Each item of str1 is:");
for(int i=0;i<str1.length();i++){
char c = str1.charAt(i); //使用charAt()方法得到String字符串中指定位置的字符,并利用循環分別輸出
System.out.print("\""+c+"\",");
if((i+1)%10==0) System.out.println();
}
System.out.println();
byte b[] = str1.getBytes();
//使用String類的getBytes()方法,得到str1中所有字符對應的Unicode編碼,并存入byte型數組b中
System.out.println("Change each item of str1 to unicode is:");
for(int i=0;i<b.length;i++){ //利用循環分別輸出str1中,所有元素對應的字符的Unicode編碼
System.out.print(b[i]+",");
if((i+1)%10==0) System.out.println();
}
System.out.println();
System.out.print("The result of comparing str2 and str3 is:");
System.out.println(str2.equals(str3));
//將字符串對象str2與str3進行比較,并將返回值輸出
System.out.println("\nReplace all charcters \"a\" to \"e\" in str2");
System.out.println("The result of str2.replace() is:"+str2.replace('a','e'));
//將字符串str2調用replace()方法的結果輸出
System.out.println("str2 is: "+str2+"\n");
System.out.print("The result of whether str1 is beginning with \"Hello\":");
System.out.println(str1.startsWith("Hello"));
System.out.print("The result of whether str3 is end with \"code.\":");
System.out.println(str3.endsWith("code."));
System.out.print("The subString of str1 whose position is arange from 2 to end is:");
System.out.println(str1.substring(2));
System.out.print("The subString of str1 whose position is arange from 2 to 5 is:");
System.out.println(str1.substring(2,5));
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -