Categories
Uncategorized

Strings in Java

String manipulation is the most common part of Many java programs. In java, string is basically an object that represents sequence of char values.

An array of characters works same as java string. For example:

  1. char[] ch={‘j’,’a’,’v’,’a’,’t’,’p’,’o’,’i’,’n’,’t’};
  2. String s=newString(ch);

is same as:

  1. String s=”javatpoint”;

String is a sequence of characters. But in java, string is an object that represents a sequence of characters. String class is used to create string object. Strings are the class objects and implemented using two classes, namely, String and StringBuffer.

The String class is immutable, so that once it is created a String object cannot be changed. If there is a necessity to make a lot of modifications to Strings of characters, then you should use String Buffer & String Builder Classes.

Java string as compared  to C Strings, are more reliable and predictable. This is basically due to C’s lack of bounds-checking. A java string is not a character array and is not NULL terminated. Strings may be declared and created as follows:

  1. String StringName;
  2. StringName= new string (“string”);

These two statements may b combined as follows:

  1. String StringName= new String ( “string”);

String Arrays

we can also create and use arrays that contain strings. the statement

  1. String itemArray []= new String[3]; // will create an itemArray of size 3 to hold three string constants.

String Methods

The String class defines a number of methods that allow us to accomplish a variety of string manipulation.

Some of the most commonly used Strings Methods are shown below:

    1. Char charAt(int index): Returns the character at the specified index.
    2. int compareToIgnoreCase(String str): Compares two strings lexicographically, ignoring case differences.
    3. String concat(String str): Concatenates the specified string to the end of this string.
    4. int compareTo(Object o): Compares this String to another Object.

    5. int indexOf(int ch)
      : Returns the index within this string of the first occurrence of the specified character.
    6. int indexOf(String str): Returns the index within this string of the first occurrence of the specified substring.
    7. int length(): Returns the length of this string.
    8. String toLowerCase(): Converts all of the characters in this String to lower case using the rules of the default locale.
    9. String toUpperCase(): Converts all of the characters in this String to upper case using the rules of the default locale.
    10. String trim(): Returns a copy of the string, with leading and trailing whitespace omitted.
    11. String concat(String str): Concatenates the specified string to the end of this string.

Example using length():

public class StringDemo

{

public static void main(String args[])

{

String palindrome = “Dot saw I was Tod”;

                      int len = palindrome.length();

System.out.println( “String Length is : ” + len );

}

}

Output: String Length is : 17

StringBuffer Class

StringBuffer is a peer class of string. The StringBuffer class is used when there is a necessity to make a lot of modifications to Strings of characters.Unlike Strings, objects of type StringBuffer can be modified over and over again with out leaving behind a lot of new unused objects.  we can insert characters and substrings in the middle of a string, or append another string to the end.

some of the methods that are frequently used in string manipulations are: 

  1. int capacity(): Returns the current capacity of the String buffer.
  2. s1.append(s2): Appends the string s2 to s1 at the end.
  3. s1.insert(n,s2): inserts the string s2 at the position n of the string s1.
  4. char charAt(int index): The specified character of the sequence currently represented by the string buffer, as indicated by the index argument, is returned.
  5. public delete(int start, int end): Deletes the string starting from start index until end index.
  6. s1.setCharAt(n, ‘x’): Modifies the nth character to x.

Example using Insert():

public class Test

{

       public static void main(String args[])

                {

                    StringBuffer sb = new StringBuffer(“abcdefghijk”);

                    sb.insert(3,”123″);

                    System.out.println(sb);

                 }

}

Output: 123defghijk

References: http://www.tutorialspoint.com

                     Books: Programming wiitten by E Balagurusamy