0基础学java_String类及常用方法

String类是编程中经常使用的字符串类

String类的两种实例化方法

  1. 直接赋值
1 package com.feimao.code;
2 
3 public class StringDemo1 {
4     public static void main(String args[]){
5         String name = "肥猫";
6         System.out.println("姓名:" + name);
7     }
8 }

      2.通过关键字new

 1 package com.feimao.code;
 2 
 3 
 4 
 5 public class StringDemo2 {
 6 
 7     public static void main(String args[]){
 8 
 9         String name = new String("肥猫");//实例化String对象
10 
11         System.out.println("姓名:" + name);
12 
13     }
14 
15 }

String的两种比较方法

  1. 一种是用“==”,比较的是地址值
 1  package com.feimao.code;
 2 
 3 
 4 
 5 public class StringDemo02 {
 6 
 7     public static void main(String args[]){
 8 
 9         String str1 = "肥猫";
10 
11         String str2 = new String("肥猫");
12 
13         String str3 = str2;
14 
15         System.out.println("str1 == str2 -->  " + (str1 == str2));
16 
17         System.out.println("str1 == str3 -->  " + (str1 == str3));
18 
19         System.out.println("str2 == str3 -->  " + (str2 == str3));
20 
21     }
22 
23 
24 
25 }

      2.另外一种是使用equals方法

 1 package com.feimao.code;
 2 
 3 
 4 
 5 public class StringDemo02 {
 6 
 7     public static void main(String args[]){
 8 
 9         String str1 = "肥猫";
10 
11         String str2 = new String("肥猫");
12 
13         String str3 = str2;
14 
15         System.out.println("str1 equals str2 -->  " + (str1.equals(str2)));
16 
17         System.out.println("str1 equals str3 -->  " + (str1.equals(str3)));
18 
19         System.out.println("str2 equals str3 -->  " + (str2.equals(str3)));
20 
21     }
22 
23 
24 
25 }

字符数组与字符串

  1. 一个字符串可以变为字符数组,同样一个字符数组也可以变成一个字符。

String提供两种方法:

将字符串转换为数组,public char toCharArray()

 1 package company.feimao.package1;
 2 
 3 public class StringApiDemo01 {
 4     public static void main(String args[]){
 5         String str = "hello";
 6         char c[] = str.toCharArray();
 7         for(int i = 0 ; i < c.length ; i++){
 8             System.out.print(c[i] + "、");
 9         }
10     }
11 }

字符数组变为字符串,public String(char[] value)

Public String(char[] value , int offset , int count)

  1. 从字符串中取出指定位置的字符

如果想要执行此操作,则此方法返回一个char类型。Public char charAt(int index)

package company.feimao.package1;



public class StringApiDemo01 {

    public static void main(String args[]){

        String str = "hello";

       System.out.println(str.charAt(4));

        }

    }
  1. 字符串与byte数组转换

Byte数组一般用于IO操作中

将字符串转换byte数组 public bite[]  getBytes()

将一个字节数组变成字符串,(分为全部转换和部分转换)

 1 package company.feimao.package1;
 2 
 3 
 4 
 5 public class StringApiDemo01 {
 6 
 7     public static void main(String args[]){
 8 
 9         String str = "hello";
10 
11         byte b[] = str.getBytes();
12 
13         System.out.println(new String(b));
14 
15         System.out.println(new String(b , 1 ,3));
16 
17         }
18 
19     }

查找指定的字符串是否存在

  1. 从头开始查找 public int indexOf String str)
  2. 从指定位置开始查找 public int indexOf(Sring st ,int fromIndex)
 1 package company.feimao.package1;
 2 
 3 
 4 
 5 public class StringApiDemo01 {
 6 
 7     public static void main(String args[]){
 8 
 9         String str = "hello";
10 
11         System.out.println(str.indexOf("l"));
12 
13         System.out.println(str.indexOf("l" , 3));
14 
15         System.out.println(str.indexOf("X")); //没有查到返回-1
16 
17         }
18 
19     }

去掉字符串前后的空格

 1 package company.feimao.package1;
 2 
 3 
 4 
 5 public class StringApiDemo01 {
 6 
 7     public static void main(String args[]) {
 8 
 9         String str = "   hello";
10 
11         System.out.println(str.trim());
12 
13 
14 
15     }
16 
17 }

从一个指定的字符中取出里面的部分内容

  1. 从指定位置从一开始截取到结束位置public String substring(int  beginindex)
  2. 截取指定范围内的字符串 public String substring(int  beginindex , int endindex)
  3. 拆分字符串
 1 package company.feimao.package1;
 2 
 3 
 4 
 5 public class StringApiDemo01 {
 6 
 7     public static void main(String args[]) {
 8 
 9         String str = "hello world";
10 
11         System.out.println(str.substring(6));
12 
13         System.out.println(str.substring(1 , 5));
14 
15 
16 
17     }
18 
19 }

如果需要按照指定字符串去拆分一个字符串,则使用 public String[] split(String regex)

  1. 大小写转换

小写字母全部转换成大写字母:public String to UpperCase()

大写字母全部转换成小写字母:public String to LowerCase()

不区分大小写比较

str1.equalsIgnoreCase(str2)

 1 public class StringAPIDemo11{
 2 
 3 public static void main(String args[]){
 4 
 5       String str1 = "HELLO" ;               // 定义字符串
 6 
 7       String str2 = "hello" ;            // 定义字符串
 8 
 9       System.out.println(""HELLO" equals "hello" " + str1.equals(str2)) ;
10 
11       System.out.println(""HELLO" equalsIgnoreCase "hello" "
12 
13                   + str1.equalsIgnoreCase(str2)) ;   // 不区分大小写的比较
14 
15 }
16 
17 }

字符串的替换

Public String replaceAll(String regex , String replacement)

 1 public class StringAPIDemo{
 2 
 3 public static void main(String args[]){
 4 
 5       String str = "hello" ;              // 定义字符串
 6 
 7       String newStr = str.replaceAll("l","x")   ;     // 现在将所有的l替换成x
 8 
 9       System.out.println("替换之后的结果:" + newStr) ;
10 
11 }
12 
13 }

常见的面试题: java中String , StringBuilder , StringBuffer的区别?