public class Test {
public static void main(String[] args) { String str = "abc123def456ghi789jklabc123def"; String s = "thinking in java"; String ss = "www.baidu.com"; String sss = " hello "; /**获取字符串长度*/ int length = str.length(); System.out.PRintln(length); /**检索给定字符在字符串中的位置及验证给定字符在字符串中是否存在*/ int a = str.indexOf("s");//当前字符串不包含传入的字符 则返回-1 int d = str.indexOf("d");//当前字符串包含传入字符 则返回该字符在字符串中出现的第一个位置 int index = s.indexOf("in",3);//表示从字符串的下标3处开始找给定字符 返回该字符在字符串中出现的第一个位置 int index2 = s.lastIndexOf("in");//表示查找给定字符在字符串中的最后一个位置 System.out.println(a);//-1 System.out.println(d);//6 System.out.println(index);//5 System.out.println(index2);//9 /**截取当前字符串的字串*/ String zifu =ss.substring(4,9);//subString方法用于截取字符串 传入两个参数表示开始下标到结束下标 String zifu2=ss.substring(4);//一个参数表示从该参数下标开始截取整个字符串 System.out.println(zifu);//baidu System.out.println(zifu2);//baidu.com /**去掉当前字符串中包含的空字符*/ System.out.println(sss);// hello String zi = sss.trim();//去掉前后空字符 System.out.println(zi);//hello /**获取字符串中给定位置的值*/ char fu = s.charAt(4);//获取字符串在4处的字母 System.out.println(fu);//k /**判断当前字符串是否以给定字符开头或结尾*/ System.out.println(ss.startsWith("ww"));//检查当前字符串是否以ww开头 此处为true System.out.println(ss.endsWith("n"));//检查当前字符串是否以n结尾 此处为false /**字符串转换大小写*/ String a1 = "i LOVE you"; System.out.println(a1.toUpperCase());//将字符串转换为大写 System.out.println(a1.toLowerCase());//将字符串转换为小写 /**将其他类型转换为字符串*/ double aa =3.1415926; int aaa = 211; System.out.println(String.valueOf(aa)+11);//3.141592611 System.out.println(String.valueOf(aaa)+11);//21111 /*使用给定正则表达式验证当前字符串是否满足格式要求,满足则返回true*/ String Email = "fancq@tedu.cn"; //[a-zA-Z0-9_]+@[a-zA-Z0-9]+(/.[A-Za-z]+)+ String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(//.[a-zA-Z]+)+"; boolean value =Email.matches(regex); if(value){ System.out.print("是邮箱"); }else{ System.out.println("不是邮箱"); } /*将当前字符串中满足正则表达式的部分替换为给定字符串*/ str = "abc123def456ghi789jkl"; str = str.replaceAll("//d+", "***"); System.out.println(str); /*将当前字符串中满足表达式的部分进行拆分(删除),并返回所有拆分后的部分*/ str = "abc123def456ghi789jkl"; String [] data = str.split("[//d]+");// [//d]+ 等同于 [0-9]+ System.out.println("数组长度为:"+data.length); for(int i=0;i<data.length;i++){ System.out.println(data[i]); }}}
新闻热点
疑难解答