刚刚复习了下String基础方法,格式化还没理解清楚。

来源:互联网 发布:男士内衣品牌推荐知乎 编辑:程序博客网 时间:2024/04/29 08:29
package StringTest;public class StringTest1{public static void main(String[] args){//连接字符串cancat()。String s = new String("hello");String ss = new String("world");String sss;System.out.println(sss = s.concat(ss));//结果为:helloworld。//获取sss的字符串长度。int v = sss.length();//打印sss.length-1长度位置的字符System.out.println(sss.charAt(v-1));//结果为:dSystem.out.println("------------------");String s1 = new String("hello");//获取字符串中指定位置的字符charAt();System.out.println(s1.charAt(0));//结果为:hSystem.out.println("------------------");//改变字符串大小写,吧s1中的字符串全部改为大写。System.out.println(s1.toUpperCase());//结果为:HELLOSystem.out.println("------------------");//分割字符串,例如在e位置分割字符串,先声名一个字符串数组接受分割后的元素。String[] s2 = s1.split("e");System.out.println(s2.length);//结果:2//迭代器遍历S2for(String value:s2){System.out.println(value);}//结果为 h llo,如果分割的是l,那么结果就是he o,两个l全部没了。System.out.println("------------------");//获取子串String a = new String("hello");String subs1 = a.substring(0, 2);System.out.println(subs1);//结果为"he"String subs2 = a.substring(2);System.out.println(subs2);//结果为"loo"System.out.println("------------------");//更改字符串中部分字符的方法//replace(char,char) replaceAll(String,String) replace(String,String)String s3 = new String("hello");//把"l" 全部替换成 "o"。String replacesult = s3.replace("l", "o");System.out.println(replacesult);//结果为:heooo。//将"ll" 统一替换成"LL"。String s4 = new String("hello");String replacesult1 = s4.replace("ll", "LL");System.out.println(replacesult1);//结果为:heLLo。//将"l"与"LL"替换String s5 = new String("hello");String replacesult2 = s5.replace("l", "LL");System.out.println(replacesult2);//结果为:heLLLLo。}}

0 0
原创粉丝点击