首页 > 技术文章 > Java基础之String类

james-zhan 2017-03-12 16:30 原文

  1、String型字符串就是多个char字符组成的一串数据。 可以将它和字符数组进行相互转换。 

  2、字符串的声明:

    1)、String 变量名 = “”;//直接声明并赋值

    2)、String 变量名 = new String("");

    3)、String 变量名;     变量名 = “”;

  3、字符串的特点:

    1)、字符串一旦被赋值就不能改变:此处的改变说的是字符串的内容不能被改变,而不是引用不能改变。

      2)、赋的值作为字符串对象和通过构造方法创建对象的方法不同:String s = new String("hello");和String s = "hello";是有区别的。

  4、字符串之间的比较:

    字符串的判断方法有:

      1)、boolean equals(Object obj):此方法比较字符串的内容是否相同,区分大小写;

      2)、boolean equalsIngoreCase(String str):此方法也是比较字符串内容是否相同,不区分大小写;

      3)、boolean contains(String str):判断大字符串中是否包含小字符串;

      4)、 boolean startsWith(String str):判断字符串是否以某个指定的字符串开头;

      5)、 boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾;

      6)、boolean isEmpty():判断字符串是否为空。空字符串的表达方式有:String str = "";或者String str = null;两种方式

    具体可以参考下面的代码示例:

 1 public class StringDemo {
 2     public static void main(String[] args) {
 3         // 创建字符串对象
 4         String s1 = "helloworld";
 5         String s2 = "helloworld";
 6         String s3 = "HelloWorld";
 7 
 8         // boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
 9         System.out.println("equals:" + s1.equals(s2));
10         System.out.println("equals:" + s1.equals(s3));
11         System.out.println("-----------------------");
12 
13         // boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
14         System.out.println("equals:" + s1.equalsIgnoreCase(s2));
15         System.out.println("equals:" + s1.equalsIgnoreCase(s3));
16         System.out.println("-----------------------");
17 
18         // boolean contains(String str):判断大字符串中是否包含小字符串
19         System.out.println("contains:" + s1.contains("hello"));
20         System.out.println("contains:" + s1.contains("hw"));
21         System.out.println("-----------------------");
22 
23         // boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
24         System.out.println("startsWith:" + s1.startsWith("h"));
25         System.out.println("startsWith:" + s1.startsWith("hello"));
26         System.out.println("startsWith:" + s1.startsWith("world"));
27         System.out.println("-----------------------");
28 
29         // 练习:boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾这个自己玩
30 
31         // boolean isEmpty():判断字符串是否为空。
32         System.out.println("isEmpty:" + s1.isEmpty());
33 
34         String s4 = "";
35         String s5 = null;
36         System.out.println("isEmpty:" + s4.isEmpty());
37         // NullPointerException
38         // s5对象都不存在,所以不能调用方法,空指针异常
39         System.out.println("isEmpty:" + s5.isEmpty());
40     }
41 }

 

    字符串中的"=="和equals区别,具体可参考下面的代码片段:

  

 1        ==和equals()
 2         String s1 = new String("hello");
 3         String s2 = new String("hello");
 4         System.out.println(s1 == s2);// false
 5         System.out.println(s1.equals(s2));// true
 6 
 7         String s3 = new String("hello");
 8         String s4 = "hello";
 9         System.out.println(s3 == s4);// false
10         System.out.println(s3.equals(s4));// true
11 
12         String s5 = "hello";
13         String s6 = "hello";
14         System.out.println(s5 == s6);// true
15         System.out.println(s5.equals(s6));// true    

    

   由于字符串可以看做是多个字符组成的数组,故可以对字符串进写插入、获取长度、获取自定位置的字符等操作,具体可以操作的功能有:

    1)、int length():获取字符串的长度;

    2)、har charAt(int index):按照索引值获得字符串中的指定字符。Java规定,字符串中第一个字符的索引值是0,第二个字符的索引值是1,依次类推;

    3)、int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引;为什么这里是int类型,而不是char类型? 原因是:'a'和97其实都可以代表'a'

    4)、int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引;

    5)、int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引;

    6)、int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引;

    7)、String substring(int start):从指定位置开始截取字符串,默认到末尾;

    8)、String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。

   具体可以参考下面的代码示例:

 1 public class StringDemo {
 2     public static void main(String[] args) {
 3         // 定义一个字符串对象
 4         String s = "helloworld";
 5 
 6         // int length():获取字符串的长度。
 7         System.out.println("s.length:" + s.length());
 8         System.out.println("----------------------");
 9 
10         // char charAt(int index):获取指定索引位置的字符
11         System.out.println("charAt:" + s.charAt(7));
12         System.out.println("----------------------");
13 
14         // int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
15         System.out.println("indexOf:" + s.indexOf('l'));
16         System.out.println("----------------------");
17 
18         // int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
19         System.out.println("indexOf:" + s.indexOf("owo"));
20         System.out.println("----------------------");
21 
22         // int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
23         System.out.println("indexOf:" + s.indexOf('l', 4));
24         System.out.println("indexOf:" + s.indexOf('k', 4)); // -1
25         System.out.println("indexOf:" + s.indexOf('l', 40)); // -1
26         System.out.println("----------------------");
27 
28         // 自己练习:int indexOf(String str,int
29         // fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
30 
31         // String substring(int start):从指定位置开始截取字符串,默认到末尾。包含start这个索引
32         System.out.println("substring:" + s.substring(5));
33         System.out.println("substring:" + s.substring(0));
34         System.out.println("----------------------");
35 
36         // String substring(int start,int
37         // end):从指定位置开始到指定位置结束截取字符串。包括start索引但是不包end索引
38         System.out.println("substring:" + s.substring(3, 8));
39         System.out.println("substring:" + s.substring(0, s.length()));
40     }
41 }

 

   String的转换功能有:

    1)、byte[] getBytes():把字符串转换为字节数组;

    2)、char[] toCharArray():把字符串转换为字符数组;

    3)、static String valueOf(char[] chs):把字符数组转成字符串;

    4)、static String valueOf(int i):把int类型的数据转成字符串;注意:String类的valueOf方法可以把任意类型的数据转成字符串

    5)、String toLowerCase():把字符串转成小写;

    6)、String toUpperCase():把字符串转成大写;

    7)、String concat(String str):把字符串拼接。

  具体用法可参考下面的示例代码:

 1 public class StringDemo {
 2     public static void main(String[] args) {
 3         // 定义一个字符串对象
 4         String s = "JavaSE";
 5 
 6         // byte[] getBytes():把字符串转换为字节数组。
 7         byte[] bys = s.getBytes();
 8         for (int x = 0; x < bys.length; x++) {
 9             System.out.println(bys[x]);
10         }
11         System.out.println("----------------");
12 
13         // char[] toCharArray():把字符串转换为字符数组。
14         char[] chs = s.toCharArray();
15         for (int x = 0; x < chs.length; x++) {
16             System.out.println(chs[x]);
17         }
18         System.out.println("----------------");
19 
20         // static String valueOf(char[] chs):把字符数组转成字符串。
21         String ss = String.valueOf(chs);
22         System.out.println(ss);
23         System.out.println("----------------");
24 
25         // static String valueOf(int i):把int类型的数据转成字符串。
26         int i = 100;
27         String sss = String.valueOf(i);
28         System.out.println(sss);
29         System.out.println("----------------");
30 
31         // String toLowerCase():把字符串转成小写。
32         System.out.println("toLowerCase:" + s.toLowerCase());
33         System.out.println("s:" + s);
34         // System.out.println("----------------");
35         // String toUpperCase():把字符串转成大写。
36         System.out.println("toUpperCase:" + s.toUpperCase());
37         System.out.println("----------------");
38 
39         // String concat(String str):把字符串拼接。
40         String s1 = "hello";
41         String s2 = "world";
42         String s3 = s1 + s2;
43         String s4 = s1.concat(s2);
44         System.out.println("s3:"+s3);
45         System.out.println("s4:"+s4);
46     }
47 }

  String型其他功能有:

    1)、替换功能:String replace(char old,char new);或者String replace(String old,String new);

    2)、去除字符串两空格:String trim();

    3)、contains() 方法用来检测字符串是否包含某个子串

 

     4)、replace()以指定字符串作为分隔符,对当前字符串进行分割,分割的结果是一个数组

 

  具体可参考下面的示例代码:

 1 public class StringDemo {
 2     public static void main(String[] args) {
 3         // 替换功能
 4         String s1 = "helloworld";
 5         String s2 = s1.replace('l', 'k');
 6         String s3 = s1.replace("owo", "ak47");
 7         System.out.println("s1:" + s1);
 8         System.out.println("s2:" + s2);
 9         System.out.println("s3:" + s3);
10         System.out.println("---------------");
11 
12         // 去除字符串两空格
13         String s4 = " hello world  ";
14         String s5 = s4.trim();
15         System.out.println("s4:" + s4 + "---");
16         System.out.println("s5:" + s5 + "---");
17 
18         // 按字典顺序比较两个字符串
19         String s6 = "hello";
20         String s7 = "hello";
21         String s8 = "abc";
22         String s9 = "xyz";
23         System.out.println(s6.compareTo(s7));// 0
24         System.out.println(s6.compareTo(s8));// 7
25         System.out.println(s6.compareTo(s9));// -16
26     }
27 }

 

1 public class Demo {
2     public static void main(String[] args){
3         String str = "wei_xue_yuan_is_good";
4         String strArr[] = str.split("_");
5         System.out.println(Arrays.toString(strArr));
6     }
7 }
8 运行结果:
9 [wei, xue, yuan, is, good]

 

 

 

 

    

 

推荐阅读