1.15 String Manipulation

3 阅读2分钟

1. Exam Points

  • Manipulate strings using methods
    • int length().
    • String substring(int from, int to)
    • String substring(int from)
    • int indexOf(String str)
    • boolean equals(Object other)
    • int compareTo(String str)
  • Note:
    • index of a string starts from 0
    • get one character from a string: substring(index, index + 1).
    • get n characters from a string: substring(index, index + n).
    • get BCD from ”ABCD.EF”: substring(1, 4)
    • check if two strings are equal: use the equals() method.

2. Knowledge Points

(1) The String Class

  • A String object represents a sequence of characters and can be created by using a string literal or by calling a String class constructor. image.png
  • The String class is part of the java.lang package. Classes in the java.lang package are available by default.
  • A String object is immutable (不可变的), meaning once a String object is created, methods called on a String object do not change the content of the String object.
    image.png
    • here we get a new string str2 by manipulating str1, but str1 remains unchanged.
  • Two String objects can be concatenated together or combined using the + or += operator, resulting in a new String object. image.png
  • Use () to avoid mistakes. c += a + b is c = c + (a+b) image.png

(2) String Manipulation

  • A String object has index values from 0 to length-1. image.png
  • Attempting to access indices outside this range will result in a StringIndexOutOfBoundsException.
  • A string identical to the single element substring at position index can be created by calling substring(index, index + 1).
    • Example:
    • String str = "ABCDEF";
    • String newStr = str.substring(2,3)
    • newStr is C
  • String methods:
    • int length(): returns the number of characters in a String object.
    • String substring(int from, int to): returns the substring beginning at index from and ending at index to - 1.
    • String substring(int from): returns substring(from, length()).
    • int indexOf(String str): returns the index of the first occurrence of str; returns -1 if not found.
    • boolean equals(Object other): returns true if this corresponds to the same sequence of characters as other; returns false otherwise.
    • int compareTo(String other): returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other. Strings are ordered based upon the alphabet.
  • Example:
    
      String str = "ABCDEFG";
      
      // 1. length()
      int len = str.length();
    
      // 2. indexOf() : int indexOf(String str)
      int index1 = str.indexOf("A"); // result: 0
      int index2 = str.indexOf("D"); // result: 3
      int index3 = str.indexOf("EF"); // result: 4
      int index4 = str.indexOf("G"); // result: -1
    
      // 3. substring(int from)
      String substring1 = str.substring(2); // result: BCDEFG
      String substring2 = str.substring(4); // result: EF
    
      // 4. substring(int from, int to)
      String substring3 = str.substring(3, 5); // result: DE
    
      // 4.1 get one character
      String email = "annabelle.li@qq.com";
      String substring4 = email.substring(0, 1); // result: a
    
      // 4.2 get two characters
      String substring5 = email.substring(0, 2); // result: an
    
      // 4.3 get three characters 
      String substring6 = email.substring(3, 6); // result: abe
    
      // 5. equals() : boolean equals(Object other)
      String str1 = "lily";
      String str2 = new String("lily");
      System.out.println(str1.equals(str2)); // true
      System.out.println(str2.equals(str1)); // true
    
      // 6. compareTo(String str)
      String x = "abc";
      String y = "axc";
      // the returned value indicates which string is larger
      // 0: x equals y
      // negative: x less than y alphabetically
      // positive: x greater than y alphabetically
      int result = x.compareTo(y); // result: a negative integer
      
    

3. Exercises