各人随笔之String的应用,很重要
String s = "helloWorld";
String s1 = "helloworld";
char ch[] ={'a','b','c','d','e','f','g'};
String ss = new String(ch,2,4);//根据需要从字符数组里取出字符串,(字符数组,开始位置,字符个数)
System.out.println(ss);
******charAt根据下标查找******
char ch1 = s.charAt(0);
System.out.println(ch1);
******contains(目标字符串)可以做为模糊匹配******
System.out.println(s.contains("hello")); //查询hello是否在字符串中
******endsWith("word")是否以某个字符串结尾******
System.out.println(s.endsWith("word"));
******startWith("hello")以某个字符串开始******//判断一个号码是不是手机号码s.startsWith("158")
System.out.println(s.startsWith("hello"));
******equals()比较字符串的内容******
******equalsIgnoreCase(String anotherString)比较字符串大小,忽略大小写******
System.out.println(s.equals(s1));
******getBytes()******
******使用平台的默认字符集将此 String
编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
byte[] by = s.getBytes();
System.out.println(s.getBytes());
for(int i = 0;i<by.length;i++){
System.out.println((char)by[i]);
}
******indexOf(String str)******
返回指定子字符串在此字符串中第一次出现处的索引。
System.out.println(s.indexOf("o"));
******length()求字符串长度******
System.out.println(s.length());
******replace(char oldchar,char newcahr)******
返回一个新的字符串,它是通过用 newChar
替换此字符串中出现的所有 oldChar
得到的。
System.out.println(s.replace('o', 'w'));
******replace(charSequence target,charSequence replacements)替换字符串******
使用指定的字面值替换序列替换此字符串所有匹配字面值目标序列的子字符串。
******split()字符串的分割******
String sm = "hello sorry nihao,world";
String[] sk = sm.split(",");//逗号分割
for(int i = 0;i<sk.length;i++){
System.out.println(sk[i]);
}
******substring(2)从某个索引位置截取字符串******
System.out.println(s.substring(2));
System.out.println(s.substring(1,4));//(开始位置,结束位置)
*******toLowCase()*******
*******toUpperCase()******
System.out.println(s.toLowerCase());//大写转换成小写
System.out.println(s.toUpperCase());//小写转换成大写
******trim()忽略首尾空白处******
String m = " 你好 我 ";
System.out.println(m.trim().length());
System.out.println(m.length());
int a = 123456789;
double b = 123.12;
String sa = s.valueOf(a);//转换成字符串
System.out.println(sa);
String sb = Double.toString(b);
System.out.println(sb);
******String:不可变字符序列*****
******StringBuffer()经常改变字符串就用它,线程安全,效率低******
******StringBuilder():可变字符序列,效率高,线程不安全*****
Stringb
StringBuffer sc = new StringBuffer("hello");
StringBuffer sd = new StringBuffer("Hello");
sd.append("word");
System.out.println(sd);
******reverse()//倒序输出字符串******
System.out.println(sd.reverse());