public class Test1 {
public static void main(String[] args) {
//定义一个字符串
String s="Hello Word";
String s1=new String();
String s2=new String("Hello Word");
//字符串的拼接
String a="hello";
String b="word";
String c=a+b;
System.out.println(c);
//char charAt(int index) 返回字符串中第index个字符。
char d=s.charAt(1);
System.out.println(d);
//int indexOf(String str) 返回字符串所在的索引
int e=s.indexOf(d);
System.out.println(e);
int e1=s.indexOf(d, 1);
System.out.println(e1);
//
}
@Test
public void test2(){
//boolean equalsIgnoreCase(String other) 比较两个字符串 不区分大小写
String s1 ="abc";
String s2 ="AbC";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
}
@Test
public void test4(){
// int length() 返回字符串的长度。
String s="sadkla";
System.out.println(s.length());
}
@Test
public void test5(){
/*String replace(char oldChar,char newChar)返回一个新串,
* 它是通过用 newChar 替换此字符串中出现的所有oldChar而生成的
* */
String str1 = "welcometozzsxt";
str1 = str1.replace('w', 'a');
System.out.println(str1);
}
@Test
public void test6(){
/*boolean startsWith(String prefix)
如果字符串以prefix开始,则返回true
boolean endsWith(String prefix)
如果字符串以prefix结尾,则返回true
* */
String str1 = "welcometozzsxt";
boolean b = str1.startsWith("welq");
System.out.println(b);
}
@Test
public void test7(){
/*
* String substring(int beginIndex)
String substring(int beginIndex,int endIndex)
返回一个新字符串,该串包含从原始字符串beginIndex到串尾或
endIndex-1的所有字符
* */
String str1 = "welcometozzsxt";
String str2 = str1.substring(7);
//System.out.println(str2);
String str3 = str1.substring(1, 3); // 参数 理解成 索引 含头不含尾
System.out.println(str3);
}
}