📄 stringdemo.java
字号:
package test;
public class StringDemo
{
String str1 = "Hello world!This is Java code.";
String str2;
String str3 = new String("This is Java code.");
public StringDemo()
{
str2 = new String("This is Java code.");
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++)
{ //使用charAt()方法得到String字符串中指定位置的字符,并利用循环分别输出
char c = str1.charAt(i);
System.out.print("\""+c+"\",");
}
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]+",");
}
System.out.println();
System.out.print("The result of comparing str2 and str3 is:");
System.out.println(str2.equals(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'));
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"));
}
public static void main(String[] args)
{
StringDemo stringDemo = new StringDemo();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -