Java笔记(三)

12. 字符串

String s1 = "abc";
String s2 = new String("abc");

 s1在内存中有一个对象;s2在内存中有两个对象。

 1 public class Demo{
 2     public static void main(String[] args) {
 3         String s1 = "abc";
 4         String s2 = new String("abc");
 5         String s3 = "abc";
 6         
 7         System.out.println(s1 == s2);
 8         System.out.println(s1 == s3);
 9     }
10 }

输出结果 false true

对字符串的常见操作:

(1)获取:int length()   获取长度

       char charAt(int index)  根据位置获取位置上的某个字符

       int indexOf(int ch)    返回的是ch在字符串中第一次出现的位置

       int indexOf(int ch, int fromIndex)  从fromIndex指定位置开始,获取ch在字符串中出现的位置

       int indexOf(String str) 

       int indexOf(String ch, int fromIndex)

       int lastIndexOf(int ch) ...

(2)判断

       boolean contains(str)   字符串中是否包含某一个子串;也可以indexOf(str) 索引str第一次出现的位置,如果返回-1,表示str不在字符串中存在。所以也可以用于对指定字符串判断是否包含。

       boolean isEmpty()   字符串中是否有内容

       boolean startWith(str)   字符串是否是以指定内容开头

       boolean endsWith(str)   字符串是否是以指定内容结尾

       boolean equals(str)   判断字符串内容是否相同。复写了Object类中的equals方法。

       boolean equalsIgnoreCase()   判断内容是否相同,忽略大小写

(3)转换

       将字符数组转成字符串

        构造函数:String(char[])

             String(char[], offset, count) 将字符数组中的一部分转成字符串

        静态方法:static String copyValueOf(char[])

             static String copyValueOf(char[], offset, count)

             static String valueOf(char[])  

      将字节数组转成字符串

        String(byte[])

        String(byte[], offset, count)  将字节数组中的一部分转成字符

      将字符串转成字符数组

        char[] toCharArray()   

      将字符串转成字节数组

        byte[] getBytes()

      将基本数据类型转成字符串

        static String valueOf(int)    static String valueOf(double)

      注:字符串和字节数组在转换过程中,是可以指定编码表的。

(4)替换

      String replace(oldchar, newchar)

(5)切割

      String[] split(regex)

(6)子串:获取字符串中的一部分

      String subString(begin)  从指定位置到结尾。如果角标不存在,会出现字符串角标异常

      String subString(begin, end)    包含头,不包含尾

(7)转换,去除空格,比较

    将字符串转成大写或小写: String toUpperCase()  String toLowerCase()

    将字符串两端的空格去除: String trim()

    对两个字符串进行自然顺序的比较: int compareTo(String) 如果参数字符串等于此字符串,则返回0;小于的话返回一个负数;大于返回一个正数。

字符串操作 例

(1)模拟一个trim方法,去除字符串两端的空格。

  思路:判断字符串第一个位置是否是空格,如果是,继续向下判断,直到不是空格为止。结尾处判断空格也是如此;当开始和结尾都判断到不是空格时,就是要获取的字符串。

 1 public class Demo{
 2     public static void main(String[] args) {
 3         String s = myTrim("   abc de ");
 4         System.out.println(s);
 5     }
 6     
 7     public static String myTrim(String str){
 8         int start = 0;
 9         int end = str.length() - 1;
10         while(start <= end && str.charAt(start++) == ' ');
11         while(start <= end && str.charAt(end--) == ' ');
12         str = str.substring(start - 1, end + 2);
13         return str;
14     }
15 }

(2)将一个字符串进行反转。将字符串中指定部分进行反转,“abcdefg”;“abfedcg”

  思路:将字符串变成数组,对数组进行反转;将反转后的数组变回字符串;只要将反转的部分的开始和结束的位置作为参数传递即可。

 1 public class Demo{
 2     public static void main(String[] args) {
 3         String s = myReverse("abcdefg", 2, 6);
 4         System.out.println(s);
 5     }
 6     
 7     public static String myReverse(String str, int start, int end){
 8         char[] arr = str.toCharArray();
 9         
10         reverse(arr, start, end);
11         
12         return new String(arr);
13     }
14     
15     public static void reverse(char[] arr, int start, int end){
16         for(int i = start, j = end - 1; i < j; i++, j--){
17             swap(arr, i, j);
18         }
19     }
20     public static void swap(char[] arr, int i, int j){
21         char tmp = arr[i];
22         arr[i] = arr[j];
23         arr[j] = tmp;
24     }
25 }

(3)获取一个字符串在另一个字符串中出现的次数。“abkkcdkkefkkskk”

 思路:定义一个计数器;获取kk第一次出现的位置;从第一次出现位置后剩余的字符串中继续获取kk出现的位置,每获取一次就计数一次;当获取不到时,计数完成。

 1 public class Demo{
 2     public static void main(String[] args) {
 3         int num = getSubCount("abkkcdkkefkkskk", "kk");
 4         System.out.println(num);
 5     }
 6     public static int getSubCount(String str, String key){
 7         int count = 0;
 8         int index = 0;
 9         while((index = str.indexOf(key)) != -1){
10             str = str.substring(index + key.length());
11             count++;
12         }
13         return count;
14     }
15 }

或者:

 1 public class Demo{
 2     public static void main(String[] args) {
 3         int num = getSubCount("abkkcdkkefkkskk", "kk");
 4         System.out.println(num);
 5     }
 6     public static int getSubCount(String str, String key){
 7         int count = 0;
 8         int index = 0;
 9         while((index = str.indexOf(key, index)) != -1){
10             index += key.length();
11             count++;
12         }
13         return count;
14     }
15 }

(4)获取两个字符串中最大相同子串。

  思路:将短的那个子串按照长度递减的方式获取到;将每获取到的子串,去长串中判断是否包含。如果包含,已经找到。

 1 public class Demo{
 2     public static void main(String[] args) {
 3         String s1 = "abcwerthelloyuiodef";
 4         String s2 = "cvhellobnm";
 5         String s = getMaxSubString(s1, s2);
 6         System.out.println(s);
 7     }
 8     
 9     public static String getMaxSubString(String s1, String s2){
10         String max = "", min = "";
11         max = (s1.length() > s2.length()) ? s1 : s2;
12         min = (max == s1) ? s2 : s1;
13         
14         for(int x = 0; x < min.length(); x++){
15             for(int y = 0, z = min.length() - x; z != min.length() + 1; y++, z++){
16                 String tmp = min.substring(y, z);
17                 if(max.contains(tmp)){
18                     return tmp;
19                 }
20 //                if(max.indexOf(tmp) != -1){
21 //                    return tmp;
22 //                }
23             }
24         }
25         return null;
26     }
27 }

StringBuffer是字符串缓冲区,是一个容器,长度可变化,并可以操作多个数据类型,最终会通过toString方法变成字符串。

(1)存储: StringBuffer append()   将指定数据作为参数添加到已有数据的结尾处

      StringBuffer insert(index, 数据)  可以将数据插入到指定index位置

(2)删除:StringBuffer delete(start, end)   删除缓冲区中的数据,包含start,不包含end

       StringBuffer deleteCharAt(index)   删除指定位置的字符

       对象.delete(0, 对象.length());   清空缓冲区

(3)获取:char charAt(int index)

      int indexOf(String str)

      int lastIndexOf(String str)

      int length()

      String substring(int start, int end)

(4)修改:StringBuffer replace(start, end, String)

      void setCharAt(int index, char ch)

(5)反转:reverse()

(6)void getChar s(int srcBegin, int srcEnd, char[] dst, int dstBegin)

StringBuffer线程同步,StringBuilder线程不同步

注:

 1 public class Demo{
 2     public static void main(String[] args) {
 3         Integer a = 127;
 4         Integer b = 127;
 5         System.out.println(a == b);
 6         
 7         Integer m = 128;
 8         Integer n = 128;
 9         System.out.println(m == n);
10     }
11 }

输出结果:true false

因为a和b指向了同一个Integer对象。当数值在byte范围内(-128~127),如果该数值已经存在,则不会重新开辟新空间。

posted @ 2017-01-16 16:44  Chris_z  阅读(199)  评论(0编辑  收藏  举报