要点Java17 String

字符串广泛应用在Java编程中,在Java中字符串属于对象,Java提供了String类来创建和操作字符串。

创建字符串

创建字符串最简单的方式例如以下:

String greeting = "Hello world!";

在代码中遇到字符串常量时,这里的值是"Hello world!",编译器会使用该值创建一个String对象。

和其他对象一样,能够使用keyword和构造方法来创建String对象。

String类有11种构造方法,这些方法提供不同的參数来初始化字符串,比方提供一个字符数组參数:

public class StringDemo{

    public static void main(String args[]){
        char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
        String helloString = new String(helloArray);  
        System.out.println( helloString );
    }
}

以上实例编译执行结果例如以下:

hello.

注意:String类是不可改变的final类,所以你一旦创建了String对象,那它的值就无法改变了。 假设须要对字符串做非常多改动,那么应该选择使用StringBuffer & StringBuilder 类。

经常用法

  • char charAt(int index) 返回指定索引处的 char 值。
  • static String copyValueOf(char[] data) 返回指定数组中表示该字符序列的 String。
  • boolean endsWith(String suffix) 測试此字符串是否以指定的后缀结束。
  • boolean equals(Object anObject) 将此字符串与指定的对象比較。
  • boolean equalsIgnoreCase(String anotherString) 将此 String 与还有一个 String 比較,不考虑大写和小写。
  • byte[] getBytes() 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
  • byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
  • int indexOf(int ch) 返回指定字符在此字符串中第一次出现处的索引。
  • int indexOf(int ch, int fromIndex) 返回在此字符串中第一次出现指定字符处的索引,从指定的索引開始搜索。
  • int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引。
  • int lastIndexOf(int ch) 返回指定字符在此字符串中最后一次出现处的索引。
  • int length() 返回此字符串的长度。
  • boolean matches(String regex)告知此字符串是否匹配给定的正則表達式。
  • String replace(char oldChar, char newChar) 返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的全部 oldChar 得到的。
  • String replaceAll(String regex, String replacement使用给定的 replacement 替换此字符串全部匹配给定的正則表達式的子字符串。
  • String[] split(String regex) 依据给定正則表達式的匹配拆分此字符串。
  • String[] split(String regex, int limit) 依据匹配给定的正則表達式来拆分此字符串。
  • boolean startsWith(String prefix) 測试此字符串是否以指定的前缀開始。
  • String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
  • String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。
  • String toLowerCase() 使用默认语言环境的规则将此 String 中的全部字符都转换为小写。
  • String toUpperCase() 使用默认语言环境的规则将此 String 中的全部字符都转换为大写。
  • String trim() 返回字符串的副本,忽略前导空白和尾部空白。
  • static String format(String format,Object... args) 使用指定的格式字符串和參数返回一个格式化字符串。

常见题目(使用JUnit演示)

@Test
public void testEquels(){
    String test = "12345";
    String test1 = "12345";
    String test2 = new String("12345");
    String test3 = new String("12345");

    String t1 = test;
    String t2 = "12"+"345";

    assertTrue(test==test1);
    assertTrue(test==t1);
    assertTrue(test==t2);

    assertTrue(test!=test2);
    assertTrue(test2!=test3);       
}

@Test
public void testNotChange(){
    String inString = "123";        
    String outString =  stubChangeString(inString);

    assertEquals("123", inString);
    assertEquals("123test", outString);
}

private String stubChangeString(String inString){
    inString += "test";
    return inString;
}

@Test
public void testSubString() {
    String test = "12345";
    assertEquals("2345", test.substring(1));
}

@Test
public void testToArrays() {
    List<String> ids = new ArrayList<>();
    ids.add("1");
    ids.add("2");
    ids.add("3");
    String[] id = ids.toArray(new String[0]);
    assertEquals(3, id.length);
    assertEquals("1", id[0]);
}

你也能够訪问http://txidol.github.io 获取很多其它的信息


原文地址:https://www.cnblogs.com/bhlsheji/p/4194482.html