java中String字符串常用方法

一 . Java String 类

  1. 在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串.
  2. 创建字符串最简单的方式如下:
    String sting = "hello";

  1. 注意 : String 类是不可改变的,所以你一旦创建了 String 对象,那它的值就无法改变了.

二 . Java String 类常用方法

  1. Java length() 方法:

语法

public int length()

返回值

返回字符串长度。

public class StringDemo {
    public static void main(String args[]) {
        String string = "helloword";
        int l = string.length();
        System.out.println( "字符串长度为 : " + l );
   }
}

编译结果为:
在这里插入图片描述

  1. 连接字符串
    String 类提供了连接两个字符串的方法 , 比较常用的是使用’+'操作符来连接字符串,例如:

public class StringDemo {
    public static void main(String args[]) {     
        String string = "Hello";     
        System.out.println(string+","+ "World!");  
    }
}

编译结果如下:
在这里插入图片描述

  1. Java charAt() 方法
    charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。

语法:

public char charAt(int index)

参数

index -------->字符的索引。

返回值

返回指定索引处的字符。

实例:

public class Test {
    public static void main(String args[]) {
        String string= "Hello,World";
        char result = string.charAt(8);
        System.out.println(result);
    }
}

结果:
在这里插入图片描述
即字符串的"Hello,World"索引8位置的字符为"r".

  1. Java compareTo() 方法:

compareTo() 方法用于两种方式的比较:
a) 字符串与对象进行比较。
b) 按字典顺序比较两个字符串。

语法

int compareTo(Object o)

int compareTo(String anotherString)

参数

a) o -------- 要比较的对象。
b) anotherString -------- 要比较的字符串。

实例:


public class Test {
 
    public static void main(String args[]) {
        String str1 = "123456";
        String str2 = "123456a";
        String str3 = "123456abc";
 
        int result = str1.compareTo( str2 );
        System.out.println(result);
      
        result = str2.compareTo( str3 );
        System.out.println(result);
     
        result = str3.compareTo( str1 );
        System.out.println(result);
    }
}

结果:
在这里插入图片描述
注意:
compareTo(String)方法中其实是从头开始,一个一个字符的比对原字符串和参数字符串中的字符,如果相同就下一个,直到字符出现不同(包括某一个字符串结束了)就计算这两个不同字符的ASCII码的差,作为返回值. 直到最后都相同就返回0.

  1. Java equals() 方法:

equals() 方法用于将字符串与指定的对象比较。

语法:

public boolean equals(Object anObject)

参数:

anObject ---------与字符串进行比较的对象。

返回值

如果给定对象与字符串相等,则返回 true;否则返回 false。

实例:

public class Test {
    public static void main(String args[]) {
        String Str1 = new String("hello");
        String Str2 = Str1;
        String Str3 = new String("hello");
        boolean i;

        i = Str1.equals( Str2 );
        System.out.println("返回值 = " + i );

        i = Str1.equals( Str3 );
        System.out.println("返回值 = " + i );
    }
}

结果:
在这里插入图片描述

  1. Java copyValueOf() 方法:

copyValueOf() 方法有两种形式:

a) public static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的字符串。

b) public static String copyValueOf(char[] data, int offset, int count): 返回指定数组中表示该字符序列的 字符串。

语法

public static String copyValueOf(char[] data)

public static String copyValueOf(char[] data, int offset, int count)

参数

data – 字符数组。
offset – 子数组的初始偏移量。
count – 子数组的长度。

返回值

返回指定数组中表示该字符序列的字符串。

实例:

public class Test {
    public static void main(String args[]) {
         char[] Str1 = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
         String Str2 = "";
  
         Str2 = Str2.copyValueOf( Str1 );
         System.out.println("返回结果:" + Str2);
  
         Str2 = Str2.copyValueOf( Str1, 2, 6 );
         System.out.println("返回结果:" + Str2);
     }
 }

结果:
在这里插入图片描述

  1. Java indexOf() 方法:

indexOf() 方法有以下四种形式:

a)public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

b)public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

c)int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

d)int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。

语法

public int indexOf(int ch )

public int indexOf(int ch, int fromIndex)

int indexOf(String str)

int indexOf(String str, int fromIndex)

参数

ch – 字符,Unicode 编码。
fromIndex – 开始搜索的索引位置,第一个字符是 0 ,第二个是 1 ,以此类推。
str – 要搜索的子字符串。

返回值

查找字符串,或字符 Unicode 编码在字符串出现的位置:
实例:


public class Main {
    public static void main(String args[]) {
        String string = "aaa456ac";  
        //查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.  
        System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在  
 
        // 从第四个字符位置开始往后继续查找,包含当前位置  
        System.out.println(string.indexOf("a",3));
        //indexOf(String str, int fromIndex); 返回结果:6  
 
        //(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99  
 
        // 从头开始查找是否存在指定的字符  
        System.out.println(string.indexOf(99));
        //indexOf(int ch);返回结果:7  
        System.out.println(string.indexOf('c'));
        //indexOf(int ch);返回结果:7  
      System.out.println(string.indexOf(97,3));//indexOf(int ch, int fromIndex); 返回结果:6  
        System.out.println(string.indexOf('a',3));//indexOf(int ch, int fromIndex); 返回结果:6  
    }
}

结果:
在这里插入图片描述

  1. Java replace() 方法
    replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。

语法

public String replace(char oldChar, char newChar)

参数

oldChar – 原字符。
newChar – 新字符。

返回值

替换后生成的新字符串。

实例:

public class Test {
    public static void main(String args[]) {
        String Str = new String("hello");

        System.out.print("结果 :" );
        System.out.println(Str.replace('o', 'T'));

        System.out.print("结果:" );
        System.out.println(Str.replace('l', 'D'));
    }
}

结果:
在这里插入图片描述

发布了2 篇原创文章 · 获赞 0 · 访问量 16

猜你喜欢

转载自blog.csdn.net/weixin_43729205/article/details/104078818
今日推荐