String类中开发常用方法
1. int length() 获取字符串的长度(字符个数)
String str = "java springboot";
int len = str.length();
System.out.println(len);
2.String trim() 去除当前字符串两边的空白字符
一般在用户名输入时运用
String str = " hello world ";
System.out.println(str);
str = str.trim();
System.out.println(str);
3.String toUpperCase() 将当前字符串中的英文部分转换为大写;
String toLowerCase() 将英文部分转为小写
String str = "我爱 I love Java ";
String upper = str.toUpperCase();
System.out.println(upper);
String lower = str.toLowerCase();
System.out.println(lower);
4.boolean startsWith(String str)判断当前字符串是否是以给定的字符串开始的;
boolean endswith(String str) 判断当前字符串是否是以给定的字符串结束的
String str = "thinking in java 几号房间号东方";
boolean starts = str.startsWith("think");
System.out.println(starts);
boolean sta = str.startsWith("t");
System.out.println(sta);
boolean star = str.startsWith("i");
System.out.println(star);
System.out.println("endsWith()以什么结束---->");
boolean ends = str.endsWith("东方");
System.out.println(ends);
5.char charAt(int index)获取对应位置的字符
String str = "thinking in java";
char c = str.charAt(9);
System.out.println(c);
char c1 = str.charAt(11);
System.out.println(c1);
6.int indexOf(String str)检索给定字符串在当前字符中的开始位置—根据字符串找开始位置
int lastIndexOf(String str)检索给定字符串在当前字符串中最后一次出现的位置
String str = "thinking in java";
int index = str.indexOf("in");
System.out.println(index);
index = str.indexOf("in",3);
System.out.println(index);
index = str.indexOf("IN");
System.out.println(index);
index = str.lastIndexOf("in");
System.out.println(index);
7.String substring(int start,int end)截取当前字符串中指定范围内的字符串(含头不含尾–包含start,但不包含end)
String str = "www.baidu.com";
String name = str.substring(4,9);
System.out.println(name);
name = str.substring(4);
System.out.println(name);