Unit 2 - Using Objects

2.7 String Methods

The String class in Java provides a wide array of methods to manipulate and examine strings. These methods make it easier to perform common operations such as searching within strings, comparing strings, extracting substrings, and changing the case of characters within the string. Here's an overview of some commonly used String methods:
2.7 String Methods

The String class in Java provides a wide array of methods to manipulate and examine strings. These methods make it easier to perform common operations such as searching within strings, comparing strings, extracting substrings, and changing the case of characters within the string. Here’s an overview of some commonly used String methods:

Length of a String

 

  • length(): Returns the length (character count) of the string.
     
    java
     
     
    String str = "Hello";
     
    int len = str.length(); // 5

 

Substring

  • substring(int beginIndex): Returns a new string that is a substring of this string, starting from beginIndex to the end of the original string.
  • substring(int beginIndex, int endIndex): Returns a new string that is a substring of this string, starting from beginIndex (inclusive) to endIndex (exclusive).
     
    java
     
    String str = "Hello, World!";
     
    String substr1 = str.substring(7); // "World!"
     
    String substr2 = str.substring(0, 5); // "Hello"

 

Comparing Strings

  • equals(Object anotherObject): Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
  • equalsIgnoreCase(String anotherString): Compares this String to another String, ignoring case considerations.
     
    java
     
    String str1 = "Hello";
     
    String str2 = "hello";
     
    boolean eq = str1.equals(str2); // false boolean
     
    eqIgnoreCase = str1.equalsIgnoreCase(str2); // true

 

Searching in Strings

  • contains(CharSequence s): Returns true if and only if this string contains the specified sequence of char values.
  • indexOf(int ch): Returns the index within this string of the first occurrence of the specified character, or -1 if the character does not occur.
  • lastIndexOf(int ch): Returns the index within this string of the last occurrence of the specified character, or -1 if the character does not occur.
     
    java
     
    String str = "Hello, World!";
     
    boolean contains = str.contains("World"); // true
     
    int indexOfO = str.indexOf('o'); // 4
     
    int lastIndexOfO = str.lastIndexOf('o'); // 8

 

Modifying Strings

  • replace(char oldChar, char newChar): Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
  • toLowerCase(): Converts all of the characters in this String to lower case using the rules of the default locale.
  • toUpperCase(): Converts all of the characters in this String to upper case using the rules of the default locale.
  • trim(): Returns a string whose value is this string, with any leading and trailing whitespace removed.
     
    java
     
    String str = " Hello, World! ";
     
    String replaced = str.replace(' ', '-'); // "-Hello,-World!-"
     
    String lower = str.toLowerCase(); // " hello, world! "
     
    String upper = str.toUpperCase(); // " HELLO, WORLD! "
     
    String trimmed = str.trim(); // "Hello, World!"

 

Concatenation

  • concat(String str): Concatenates the specified string to the end of this string.
     
    java
     
    String str1 = "Hello, ";
     
    String str2 = "World!";
     
    String concatenated = str1.concat(str2); // "Hello, World!"
     
    Alternatively, you can use the + operator for concatenation, which is more commonly used.

 

String Conversion and Other Methods

  • valueOf(Various Types): This is a static method that returns the string representation of the passed argument. Overloaded versions exist for various data types.
     
    java
     
    int num = 100;
     
    String strNum = String.valueOf(num); // "100"

 

These are just a few of the many methods provided by the String class in Java. Understanding and effectively using these methods can significantly enhance your ability to manipulate and process string data in your Java applications.

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *