String 类的常用字符串方法
public class Page106 {/*** 字符串练习第五章* @param args*/public static void main(String[] args) {String str=new String(" WOrld ");String str1=new String("w,o,r;l;d");System.out.println(str.length());//获取长度System.out.println(str.toUpperCase());//转为大写System.out.println(str.toLowerCase());//转为小写System.out.println(str.trim());//去掉首尾的空格 System.out.println(str.trim().length());System.out.println(str.equals(str1));//比较是否相等,区分大小写System.out.println((str.trim()).equalsIgnoreCase(str1));//不区分大小写//查找System.out.println(str.indexOf("or"));//返回第一次出现这个字符的位置,如果没有找到返回-1System.out.println(str.lastIndexOf('l'));//返回最后一次出现这个字符的位置System.out.println(str.lastIndexOf("lp",4));//从字符串第四个索引位置开始找lp最后一次出现的位置System.out.println(str1.charAt(3));//返回索引位置为3的字符//截取System.out.println(str.substring(2, 4));//截取索引位置为2-4之间的字符//分割String[] array1=str1.split(",");String[] array2=str1.split(";",3);//分割;标志的字符串,指定分割字符数为3,返回的字符存放在字符串数组中for(int i=0;i){System.out.print(array1[i]+"\t");}System.out.println();for(int i=0;i ){System.out.print(array2[i]+"\t");}//替换String s="hello world,hello world";System.out.println(s.replace("world", "java"));//用java替换所有的worldSystem.out.println(s.replaceFirst("world", "java"));//用java只替换第一次出现的worldSystem.out.println(s.replaceAll("world", "java"));//用将咪表字符串匹配的正则表达式的所有子字符串替换成新的//链接System.out.println(s.concat(str1));//在s字符串后链接str1字符串 } }
结果显示:
7WORLD world WOrld 5 false false -1 4 -1 , Or w o r;l;d w,o,r l d hello java,hello java hello java,hello world hello java,hello java hello world,hello worldw,o,r;l;d
转载于:https://www.cnblogs.com/tjlgdx/p/5993541.html
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
