- public char charAt(int index) : 返回index.指定的索引处的字符
- public int length() : 返回此字符串的长度.这里需要和获取数组长度区分开来, 数组长度是通过 "数组名.length" 获取的
- public int indexOf(String str): 返回指定子字符串str在此字符串中第一次出现处的索引.
- public int indexOf(String str, int fromIndex) : 返回指定子字符串str在字符串中第一次出现处的索引,从指定的索引fromIndex处开始搜索.
- public boolean equalsIgnoreCase(String another) : 将此String 与另一个字符串another比较,比较时不区分大小写
- public String replace(char oldChar, char ,newChar) :返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的
- public boolean startsWith(String prefix) : 判断此字符串是否以prefix指定的前缀开始.
- public boolean endsWith(String suffix): 判断此字符串是否以suffix指定的后缀结束
- public String toUpperCase() : 将String中所有的字符都转换成大写
- public String tolowerCase() : 将String 中所有的字符都转换成小写
- public String substring(int beginIndex):返回一个从beginIndex开始到结尾的新的子字符串.
- public String substring(int beginIndex, int endIndex): 返回一个从beginIndex开始到endIndex结尾(不包含endIndex所指字符)的新的子字符串.
- public String tirm(): 返回字符串的副本
- public static String ValueOf(基本数据类型): 返回基本数据类型参数的字符串表现形式
- public String[] split (String regex) : 通过regex指定的分隔符分隔字符串,返回分隔后的字符串数组.
public static void main(String[] args) {
String s1 ="gg bond";
String s2 ="GG BOND";
System.out.println(s1.charAt(1)); //根据下标查询字符 ,结果为g
System.out.println(s1.length()); //查询s1的长度
System.out.println(s1.indexOf("bond")); //查询"bond"在s1中的位置
System.out.println(s1.indexOf("BOND")); //查询"BOND"在s1中的位置
System.out.println(s1.equals(s2)); //区别大小写
System.out.println(s1.equalsIgnoreCase(s2));//不区别大小写
String s = "慕尔如星,愿****";
System.out.println(s);
String str= s.replace('星','辰');
System.out.println(str);
}
public static void main(String[] args) {
String fileName = "20200804 猫小黑 Resume.docx";
System.out.println(fileName.startsWith("20200")); //boolean类方法,判断是否以什么开头
System.out.println(fileName.endsWith("docx")); //boolean类方法, 判断是否以什么结尾
System.out.println(fileName.endsWith("doc"));
System.out.println(fileName.toLowerCase()); //将小写替换成大写
System.out.println(fileName.toUpperCase()); //将大写替换成小写
System.out.println(fileName.substring(8)); //从第九个位置开始到结尾截取字符
System.out.println(fileName.substring(8,12)); //从第九个位置截取到第十一个位置
String fileName2=" 20200804 猫小黑 Resume .docx";
System.out.println(fileName2.trim()); //忽略原字符串前后的空格
}
public static void main(String[] args) {
String result = String.valueOf(100);
Scanner in =new Scanner(System.in);
System.out.print("请输入您去年一年的薪水总和:");
int lastSalary = in.nextInt();
//通过String类的静态方法将lastSalary转换成字符串
String strSalary = String.valueOf(lastSalary);
System.out.println("您去年一年的薪水总和是:"+strSalary.length()+"位数!");
String date = "Mary,F ,1976";
String[]spiltStr= date.split(",");//用","将字符串风格成一个新的字符串数组
System.out.println("分隔后的结果为:");
for (int i=0;i<spiltStr.length;i++){
System.out.println(spiltStr[i]);
}
}