More on Java Strings

  1. Special Note:


    Java Strings are immutable:   click here (local backup)

    Method in java.lang.String do NOT alter the String.
    Some method in java.lang.String returns a new String that is created using the old String.


  2. Example 1: substring


    String substring(int beginIndex): Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string. String substring(int beginIndex, int endIndex) Returns a new string that is a substring of this string. The substring begins at the specified "beginIndex" and extends to the character at index "endIndex - 1". "Hello".substring(1) returns "ello" "Hello".substring(0,4) returns "Hell"
    Example program that uses substring: click here

  3. Example 2: concat


    String concat(String str): Concatenates the specified string (str) to the end of this string. ...a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string (str). "Hello".concat("World") returns "HelloWorld"
    Example program that uses concat: click here

    A "nicer" way to do concat: click here