Sunday, February 26, 2012

Java - String Object

A string represents a group of characters. Strings are "String" class objects in Java.
Creating Strings
1. We can create a string by declaring and initializing a String class object directly.
   Eg: String str = "Hello";

2. We can create a "String" class object using new operator
   Eg: String str = new String("Hello");

3. We can create objects by converting character arrays into String objects.
   Eg: char arr[] = {'a', 'b', 'c', 'd', 'e'};
         String s1 = new String(arr);
         String s2 = new String(arr, 1, 3);

Methods of a class java.lang.String
1. String concat(String str): Concatenates the calling string with str.We can achieve this by plus (+) operator as well.

2. int length(): returns the length of a String.

3. char charAt(int index): returns the character at the specified location. Java string index starts from "0".

4. int compareTo(String str): returns a negative value if the calling string comes before str in dictionary order,  a positive value if the string comes after str, or 0 if the strings are equal.

5. boolean equals(String str): returns true if the calling string equals to the str.
    Eg: String s1 = "Boy";
          String s2 = "Box";
          int n = s1.compareTo(s2); // Returns a positive value

6. boolean equalsIgnoreCase(String str): this is similar to equals but this method does case insensitive comparison.

7. boolean startsWith(String prefix): returns true if the calling string starts with prefix.
    Eg: String str = "He is a boy";
          boolean x = str.startsWith("He");  //Returns true

8. boolean endsWith(String suffix): returns true if the calling string ends with suffix.

    Eg: String str = "He is a boy";
          boolean x = str.endsWith("boy"); //Returns true
Note: Both startsWith and endsWith use case sensitive comparison.

9. int indexOf(String str): returns the first occurance of str in the string.

    Eg: String str = "He is a boy";
          int n = str.indexOf("This is a book"); //Returns 2


10. int lastIndexOf(String str): returns the last occurance of str in the string.

    Eg: String str = "This is a book";
          int n = str.indexOf("is"); //Returns 5
Note: Both indexOf and lastIndexOf methods returns negative value, if str not found in the calling string. Counting starts from 0.

11. String replace(char oldchar, char newchar): returns a new string that is obtained by replacing all characters oldchar in the string with newchar.

12. String substr(int beginIndex): returns a new String consisting of all characters from beginIndex until the end of the string.


13. String substr(int beginIndex, int endIndex): returns a new string consisting of all characters from beginINdex until endIndex (exclusive).

14. String toLowerCase(): converts all characters into lower case.

15. String toUpperCase(): converts all characters into upper case.

16. String trim(): eliminates all leading and trailing spaces.

StringDemo.java
/**
 * A demo class to explain about String object
 * @author Santhosh Reddy Mandadi
 * @since 26-Feb-2012
 * @version 1.0
 */

public class StringDemo
{
 public static void main(String args[])
 {
  //Creating 3 strings using 3 different ways
  String s1 = "This is Java";
  String s2 = new String("I like it");
  char arr[] = {'S','a','n','t','h','o','s','h'};
  String s3 = new String(arr);
  //Displaying strings
  System.out.println("s1 = "+s1);
  System.out.println("s2 = "+s2);
  System.out.println("s3 = "+s3);
  //Find no of chars in s1
  System.out.println("Length of s1 = "+s1.length());
  //Joining s1 and s2 strings
  System.out.println("s2 joined with s1 = "+s1.concat(s2));
  //Joining 3 strings using +
  System.out.println(s1+" by "+s3);
  //check if s1 starts with this
  boolean x = s1.startsWith("This");
  if(x)
  {
   System.out.println("s1 starts with This");
  }
  else
  {
   System.out.println("s1 doesn't start with This");
  }
  //Extracting substring from s2 and s3
  String p = s2.substring(0, 6);
  String q = s3.substring(0);
  System.out.println(p+" "+q);
  //Convert the case of s1
  System.out.println("Upper case version of s1 = "+s1.toUpperCase());
  System.out.println("Lower case version of s1 = "+s1.toLowerCase());
 }
}
Output:

s1 = This is Java
s2 = I like it
s3 = Santhosh
Length of s1 = 12
s2 joined with s1 = This is JavaI like it
This is Java by Santhosh
s1 starts with This
I like Santhosh
Upper case version of s1 = THIS IS JAVA
Lower case version of s1 = this is java



Above program has been tested in 1.5 and 1.6.

Click here to check out other programs that I've developed.

0 comments:

Post a Comment