Using String Method

2.4 Using String Method

Like other Java inbuilt classes String class also provides numerous predefined methods. A method is a collection of instructions. When a method executes, it accomplishes something.
The general expression to use a predefined method on a Reference variable is:
ReferenceVariable.MethodName(parameters)
The following statement calculates the length of string by using predefined method length().
String message = "The Java Tutorials";
int n = message.length();
In the statement, message is a reference variable. Value of the expression message.length() returns 18 which is then stored in variable n.
/**
 *  This program demonstrates
 *  length String methods.
 */
public class StringLengthDemo
{
   public static void main(String[] args)
   {
      String message;    // To hold a string
      int n;            // To hold length of String

      message = "The Java Tutorials";
      n = message.length();
      System.out.println("The length of message : " + n);
   }
}
Output :
The length of message : 18

The String class is immutable, so that once it is created a String object cannot be changed. The String class has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.
String toLowerCase()
Returns the string that is the same as this string, except  that all uppercase letters of this string are replaced with  their equivalent lowercase letters Example: message.toLowerCase() returns "the java tutorials"
String toUpperCase()
Returns the string that is the same as this string, except  that all lowercase letters of this string are replaced with  their equivalent uppercase letters  Example: message.toUpperCase() returns "THE JAVA TUTORIALS"
String concat(String str)
Returns the string that is this string concatenated with str Example: The expression message.concat(" For Beginners")  returns the string "The Java Tutorials For Beginners".
String substring (int beginIndex)
Returns the string which is a substring of this string beginning at beginIndex until the end of the string. Example: message.substring(4) returns the string "Java Tutorials"
String substring(int beginIndex, int endIndex)
Returns the string which is a substring of this string  beginning at beginIndex until endIndex – 1
Example: message.substring(4,18) returns the string "Java Tutorials"

/**
 *  This program demonstrates
 *  various String methods.
 */
public class StringMethodsDemo
{
   public static void main(String[] args)
   {
      String message;

      message = "The Java Tutorials";
      System.out.println(message.toLowerCase());
      System.out.println(message.toUpperCase());
      System.out.println(message.concat(" For Beginners"));
      System.out.println(message.substring(4));
      System.out.println(message.substring(4, 8));
   }
}
Output
the java tutorials
THE JAVA TUTORIALS
The Java Tutorials for Beginners
Java Tutorials
Java

No comments:

Post a Comment

Pages