//获取字符串的长度int length ="abc".length();//根据索引获取字符串中的某个字符
String s ="abcdefg";char ch = s.charAt(s.length()-1);
System.out.println(ch);//indexOf(),获取这个字符,第一次在字符串出现的索引,从前往后检索int index ="abcdefgafasdfljiodfasdfasfd".indexOf('g');
System.out.println(index);//返回 -1 代表没有找到int index2 ="abcdefgafasdfljiodfasdfasfd".indexOf("f2");
System.out.println(index2);//lastIndexOf() 从后往前检索,该字符第一次在字符串中出现的索引int last ="asdfasdfasdfefadfeafassdf".lastIndexOf('a');
System.out.println(last);//判断一个字符,在该字符串中只出现过一次
String str="asfarsdfridfaerfadfeadfeadfeasdfe";if(str.indexOf('i')==str.lastIndexOf('i')){
System.out.println(true);}// public int indexOf (String str,int fromIndex):// 返回指定字符串在此字符串中从指定位置后第一次出现后,再次出现的索 引。
String str ="bcdefadasfaewwfaaaa";int index = str.indexOf('a', str.indexOf('a')+1);
System.out.println(index);//从指定索引 开始 截取后面所有的内容返回一个新的字符串
String s ="好好学习,天天向上".substring(2);
String s ="好好学习,天天向上".substring(5,9);//含头不含尾
String s1 ="好好学习,天天向上";
String s = s1.substring(s1.indexOf('天'), s1.indexOf('向')+1);
System.out.println(s);//遍历字符串
String str="asdfasdffw3fasfalksfdioeasddsfeafsf";for(int i =0; i < str.length(); i++){char c = str.charAt(i);
System.out.println(c);}//大小写字母的转换
String s ="abc".toUpperCase();//转大写
String s1 ="ABC".toLowerCase();//转小写
String str ="你好";//utf-8占三个字节; gbk占两个字节;byte[] bytes = str.getBytes();//转成字节数组;//从字节数字中取出"你"
String s =newString(bytes,0,3);//从开始索引到终止索引,从字节数组获取字符串;
System.out.println(s);//输出字符串;
//比较两个字符串的内容是否相同boolean b ="abc".equals("abc");
System.out.println(b);//1.按照字典ASCII码顺序比,比差值;通过字典ASCII码顺序比不出来,就用长度比;int num ="abc".compareTo("abcdef");
System.out.println(num);//2. public int compareToIgnoreCase (String str)跟上面一样 只是忽略大小写的比较int n ="abc".compareToIgnoreCase("aBC");
System.out.println(n);