package com.ethjava; public class Stringlianxi { public static void main(String[] args) { String str = "You are the great one!"; //substring(int beginIndex) 形式 //此方式用于提取从索引位置开始至结尾处的字符串部分。 //调用时:括号中是需要提取字符串的开始位置,方法的返回值是提取的字符串。 System.out.println(str.substring(4));//are the great one! System.out.println(str.substring(3));// are the great one! 第一位是空格 //ubstring(int beginIndex,int endIndex) 形式 //此方法中的 beginIndex 表示截取的起始索引,截取的字符串中包括起始索引对应的字符; // endIndex 表示结束索引,截取的字符串中不包括结束索引对应的字符 System.out.println(str.substring(4, 6));//ar //去掉字符串的首尾空格 String str2 = " yxf are great! ";//前后有两个空格 String str3 = str2.trim(); System.out.println(str3);//yxf are great! System.out.println(str2.length());//16 System.out.println(str3.length());//14 //字符串名.length();获取字符串的长度 //连接字符串 //用+号,使用“+”运算符连接字符串和 int 型(或 double 型)数据时,“+”将 int(或 double)型数据自动转换成 String 类型。 String str4 = "Welcome to" + "Beijing" + 2019 + "欢迎来到" + "北京。"; System.out.println(str4); //Welcome toBeijing2019欢迎来到北京。 //使用concat()方法 //字符串 1.concat(字符串 2); // 字符串 2 被连接到字符串 1 后面,形成新的字符串。 String str5 = "某女生"; System.out.println(str5.concat(",你好啊")); //某女生,你好啊 //转换大小写 String str6 = "abcdef 我 MNJKL"; System.out.println(str6.toLowerCase()); // 输出:abcdef 我 mnjkl System.out.println(str6.toUpperCase()); // 输出:ABCDEF 我 MNJKL //非字母的字符不受影响 //字符串的分割 String Colors = "Red,Black,White,Yellow,Blue"; String[] arr1 = Colors.split(","); // 不限制元素个数 String[] arr2 = Colors.split(",", 3); // 限制元素个数为3 System.out.println("所有颜色为:"); for (int i = 0; i < arr1.length; i++) { System.out.println(arr1[i]); } //所有颜色为: //Red //Black //White //Yellow //Blue System.out.println("前三个颜色为:"); for (int j = 0; j < arr2.length; j++) { System.out.println(arr2[j]); } //前三个颜色为: //Red //Black //White,Yellow,Blue //String 类的 split() 方法可以按指定的分割符对目标字符串进行分割,分割后的内容存放在字符串数组中。该方法主要有如下两种重载形式: //str.split(String sign) //str.split(String sign,int limit) //str 为需要分割的目标字符串。 //sign 为指定的分割符,可以是任意字符串。 //limit 表示分割后生成的字符串的限制个数,如果不指定,则表示不限制,直到将整个目标字符串完全分割为止。 //“.”和“|”都是转义字符,必须得加“\\”。 //如果用“.”作为分隔的话,必须写成String.split("\\."),这样才能正确的分隔开,不能用String.split(".")。 //如果用“|”作为分隔的话,必须写成String.split("\\|"),这样才能正确的分隔开,不能用String.split("|")。 //替换字符串中的某些字符(串) //replace() 方法用于将目标字符串中的指定字符(串)替换成新的字符(串),其语法格式如下: //字符串.replace(String oldChar, String newChar) //其中,oldChar 表示被替换的字符串;newChar 表示用于替换的字符串。replace() 方法会将字符串中所有 oldChar 替换成 newChar。 //用于单个的字符或者字符串 String a = "hello java,hello me! \\\\"; System.out.println(a.replace("hello", "你好"));//你好 java,你好 me! \\ System.out.println(a.replaceFirst("hello", "你好"));//你好 java,hello me! \\ System.out.println(a.replaceAll("hello", "好好"));//好好 java,好好 me! \\ System.out.println(a.replace("a", "9"));//hello j9v9,hello me! \\ System.out.println(a.replace("!", "?"));//hello java,hello me? \\ System.out.println(a.replace("\\", "\\\\\\\\"));//hello java,hello me! \\\\\\\\???? System.out.println(a.replace("\\\\", "\\\\\\\\"));//hello java,hello me! \\\\???? //字符串.replaceAll(String regex, String replacement), // 其中,regex 表示正则表达式,replacement 表示用于替换的字符串 //何为正则表达式 //???? //字符串的比较: //方法1 equals()方法 String str0 = "abc"; String str02 = new String("abc"); String str03 = "ABC"; System.out.println(str0.equals(str02));//true System.out.println(str0.equals(str03));//false System.out.println(str0.equalsIgnoreCase(str03));//true //equalsIgnoreCase() 方法的作用和语法与 equals() 方法完全相同,唯一不同的是 equalsIgnoreCase() 比较时不区分大小写 //方法2:== System.out.println(str0 == str02);//false // equals() 方法和==运算符执行的是两个不同的操作 // equals() 方法比较字符串对象中的字符。而==运算符比较两个对象引用看它们是否引用相同的实例。 //变量 s1 指向由“abc”创建的字符串实例。s2 所指的的对象是以 abc作为初始化而创建的。 // 因此这两个字符串对象的内容是一样的。但它们是不同的对象,这就意味着 s1 和 s2 没有指向同一的对象,因此它们是不==的。 //方法3:compareTo();经常应用到排序中 //compareTo() 方法用于按字典顺序比较两个字符串的大小,该比较是基于字符串各个字符的 Unicode 值。 //str.compareTo(String otherstr); //它会按字典顺序将 str 表示的字符序列与 otherstr 参数表示的字符序列进行比较。 // 如果按字典顺序 str 位于 otherster 参数之前,比较结果为一个负整数; // 如果 str 位于 otherstr 之后,比较结果为一个正整数; // 如果两个字符串相等,则结果为 0 System.out.println(str0.compareTo(str03));//32 System.out.println(str03.compareTo(str0));//-32 System.out.println(str0.compareTo(str02));//0 //A的ASCII为65,a的ASCII为97 //字符串的遍历 String yy = "you are you, not me!"; for (int i = 0; i < yy.length(); i++) { char temp = yy.charAt(i); System.out.print(temp);//you are you, not me! } System.out.println(); char ch[] = yy.toCharArray(); for (int i = 0; i < ch.length; i++) { System.out.print(ch[i]);//you are you, not me! } //字符串的查找: //indexOf() 方法用于返回字符(串)在指定字符串中首次出现的索引位置,如果能找到,则返回索引值,否则返回 -1。 //str.indexOf(value) //str.indexOf(value,int fromIndex) //从头或者指定位置开始找 //其中,str 表示指定字符串;value 表示待查找的字符(串);fromIndex 表示查找时的起始索引, // 如果不指定 fromIndex,则默认从指定字符串中的开始位置(即 fromIndex 默认为 0)开始查找。 String you="how great are you?"; int index0=you.indexOf("ou");//搜索字符串 int index1=you.indexOf('a');//搜索字符 int index2=you.indexOf('a',10);//搜索范围包括开始搜索位置的字符 System.out.println(index0);//15,o的索引值 System.out.println(index1);//7 System.out.println(index2);//10 //lastIndexOf() 方法用于返回字符(串)在指定字符串中最后一次出现的索引位置,如果能找到则返回索引值,否则返回 -1。 //str.lastIndexOf(value) //str.lastlndexOf(value, int fromIndex) //注意:lastIndexOf() 方法的查找策略是从右往左查找,如果不指定起始索引,则默认从字符串的末尾开始查找。 System.out.println(you.lastIndexOf("ou"));//15 System.out.println(you.lastIndexOf('a'));//10 System.out.println(you.lastIndexOf('a',9));//7 ,从9往前的范围搜索,因为是从末尾搜索的 //String 类的 charAt() 方法可以在字符串内根据指定的索引查找字符 //字符串名.charAt(索引值) System.out.println(you.charAt(0));//h //空字符串 //""是一个长度为 0 且占内存的空字符串,在内存中分配一个空间,可以使用 Object 对象中的方法。 // null 是空引用,表示一个对象的值,没有分配内存,调用 null 的字符串的方法会抛出空指针异常 //检查一个字符串是否为空 String oo=""; if (oo.length() == 0){ System.out.println("空字符串");//空字符串 } if(oo.equals("")){ System.out.println("this is also kong");//this is also kong } String qq=null; if(qq==null){ System.out.println("this is null String");//this is null String } String str11 = new String(); String str22 = null; String str33 = ""; System.out.println(str33.length()); // 空字符串""的长度为0 //System.out.println(str22.length()); // 抛出空指针异常 //Exception in thread "main" java.lang.NullPointerException System.out.println(str11); // 输出"" System.out.println(str11 == str22); // 内存地址的比较,返回false System.out.println(str11.equals(str22)); // 值的比较,返回false System.out.println(str22 == str33); // 内存地址的比较,返回false System.out.println(str33.equals(str22)); // 值的比较,返回false System.out.println(str11 == str33); // 内存地址的比较,返回false System.out.println(str11.equals(str33)); // 值的比较,返回true } }
参考:http://c.biancheng.net/view/5823.html