📄 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 + -