零EVA

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一个简单的字符串小例子,代码中有注释

 1 class StringDemo
 2 {
 3     public static void main(String[] args)
 4     {
 5         String s1 = "abc";//s1是一个类类型变量,"abc"是一个对象
 6                           //字符串最大的特点:一旦被初始化,就不可以被改变
 7         String s2 = new String("abc");
 8         //s1和s2有什么区别?
 9         //s1在内存中有一个对象
10         //s2在内存中有两个对象
11         System.out.println(s1 == s2);
12         System.out.println(s1.equals(s2));//String类复写了equals方法,定义了自己独特的内容
13                                           //该方法用于判断字符串是否相同
14     }
15 }
View Code

 String类适用于描述字符串事物

那么它就提供了多个方法对字符串进行操作

常见的操作有哪些?

''abcd"

1 获取
  1.1 字符串中包含的字符数,也就是字符串的长度
    int length():获取长度
  1.2 根据位置获取位置上的某个字符
    char charAt(int index)
  1.3 根据字符获取该字符在字符串中的位置
    int indexOf(int ch):返回的是ch在字符串中第一次出现的位置
    int indexOf(int ch, int fromIndex):从formIndex指定位置开始,获取ch在字符串中出现的位置

    int indexOf(String str):返回的是str在字符串中第一次出现的位置
    int indexOf(String str, int fromIndex):从formIndex指定位置开始,获取str在字符串中出现的位置

2 判断
  2.1 字符串中是否包含某一个子串
    boolean contains(str);
    特殊之处:indexOf(str):可以索引str第一次出现的位置,如果返回-1,表示str不在字符串中存在
    所以,也可以用于对指定判断是否包含
    if(str.indexOf("aa")!=-1)

    而且该方法既可以判断,又可以获取出现的位置
  2.2 字符串中是否有内容
    boolean isEmpty():原理就是判断长度是否为0
  2.3 字符串是否是以指定内容开头
    boolean startsWith(str);
  2.4 字符串是否是以指定内容结尾
    boolean endsWith(str);
  2.5 判断字符串的内容是否相同,复写了object类中的equals方法
    boolean equals(str)
  2.6 判断内容是否相同,并忽略大小写
    boolean equalsIgnoreCase();

3 转换

  3.1 将字符数组转成字符串

    构造函数:String(char[ ])

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

    静态方法:

        Static String copyValueOf(char [ ]);

        Static String copyValueOf(char [ ] data,int offset,int count);

        Static String valueOf(char [ ]);

  3.2 将字符串转成字符数组

    char[ ]  toCharArray():

  3.3 将字节数组转成字符串   

        String(byte[ ])

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

  3.4 将字符串转成字节数组

    byte[ ]  getBytes();

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

    static String valueOf(int)

    static String valueOf(double)

    3+"";//String.valueOf(3);

    特殊:字符串和字节数组在传唤过程中,是可以指定编码表的

4 替换

  String  replace(oldchar,newchar);

5 切割

  String[ ]  split(regex);

6 子串,获取字符串中的一部分

  String substring(begin);

  String substring(begin,end);

 7 转换,去除空格,比较

  7.1 将字符串转成大写或者小写

    String toUpperCase();

    String toLowerCase();

  7.2 将字符串两端的多个空格去除

    String trim();

  7.3 对两个字符串进行自然顺序的比较

    int compareTo(String);

 1 class Demo
 2 {
 3     public static void method_7()
 4     {
 5         String s = "   Hello Java   ";
 6         sop(s.toLowerCase());
 7         sop(s.toUpperCase());
 8         sop(s.trim());
 9         
10         String s1 = "abc";
11         String s2 = "aaa";
12         sop(s1.compareTo(s2));
13     }
14     public static void method_sub()
15     {
16         String s = "abcdef";
17         sop(s.substring(2));//从指定位置到结尾,如果角标不存在,则会出现字符串角标越界异常
18         sop(s.substring(2,4));//包含头,不包含尾
19     }
20     public static void method_split()
21     {
22         String s = "zhangsan,lisi,wangwu";
23         String[] arr = s.split(",");
24         for(int x = 0; x < arr.length; x++)
25             sop(arr[x]);
26     }
27     public static void method_replace()
28     {
29         String s = "hello java";
30         //String s1 = s.replace('a','n');
31         String s1 = s.replace("java","C++");
32         sop("s = "+s);
33         sop("s1 = "+s1);
34     }
35     public static void method_trans()
36     {
37         char[] arr = {'a','b','c','d','e','f'};
38         String s = new String(arr,1,3);
39         sop("s = "+ s);
40         
41         String s1 = "zxcvbnm";
42         char[] chs = s1.toCharArray();
43         for(int x = 0; x < chs.length; x++)
44             sop("ch="+chs[x]);
45     }
46     public static void method_is()
47     {
48         String str = "ArrayDemo.java";
49         
50         sop(str.startsWith("Array"));
51         sop(str.endsWith(".java"));
52         sop(str.contains("Demo"));
53     }
54     
55     public static void method_get()
56     {
57         String str = "abcdefakpf";
58         
59         //长度
60         sop(str.length());
61         
62         //根据索引获取字符
63         sop(str.charAt(4));//当访问到字符串不存在的角标时会发生StringIndexOutOfBoundsException
64         
65         //根据字符获取索引
66         sop(str.indexOf('a',3));//如果没有找到,返回-1
67         
68         //反向索引一个字符出现的位置
69         sop(str.lastIndexOf('a'));
70     }
71     public static void main(String[] args)
72     {
73         method_7();
74         //method_sub();
75         //method_split();
76         //method_replace();
77         //method_trans();
78         //method_get();
79         //method_is();
80         //System.out.println("");
81     }
82     
83     public static void sop(Object obj)
84     {
85         System.out.println(obj);
86     }
87 }
View Code

 

posted on 2017-06-24 10:47  零EVA  阅读(336)  评论(0编辑  收藏  举报