public class string基础 {
public static void main(String[] args) {
String s1="hello word";
String s2=new String(s1); //由一个字符串创建另一个字符串
System.out.println("s1:"+s1);
System.out.println("s2:"+s2);
char [] c={'a','b','c'};
String s=new String(c); //用字符数组建立字符串
//链接二个字符串
s1=s1+s2; //或者s1=s1.concat(s2);
System.out.println("字符串s1+s2:"+s1);
//判断二个字符串的具体的值是否相等
String a="hello";
String b="HELLO";
System.out.println("a.equals(b):"+a.equals(b));
a.equalsIgnoreCase(b); //忽略字符串的大小写
//检索字符串出现的位置:
System.out.println("l第一次出现的位置:"+a.indexOf("l"));
System.out.println("l最后一次出现的位置:"+a.lastIndexOf("l"));
//字符串的截取
String num="012345";
System.out.println(num.substring(1));//从当前位置,截取到最后
System.out.println(num.substring(1,4)); //从【star,end);
//大小写的转换
System.out.println("大写转小写:"+b.toLowerCase());
System.out.println("小写转大写:"+a.toUpperCase());
//字符串的转换
System.out.println(a.replace("l","w"));
System.out.println(a.replaceAll("h","o"));
//数值转化成字符串串
Double shuZi=3.1415926;
String shuizi1=String.valueOf(shuZi);
System.out.println(shuizi1+1);
//字符串转化成数值:Integer.parseInt(ziFuChuan);
String ziFuChuan="12345";
int x=Integer.parseInt(ziFuChuan);
System.out.println(x);
}
}
运行结果: