Btechadda.com

OOPS/JAVA:String Handling

by hkesavaraju on Feb.27, 2010, under Java

String Handling: Java considers string as an Object where as it is treated as array of characters in many other languages. A string is a sequence of characters.

Once a string is created, that can’t be changed. It is possible to perform all types of string operations but a new string is created that contains the altered version of the string each time when a modification is performed. Therefore, a string is a fixed, immutable character sequence.

The String and Stringbuffer are defined in java.lang package. These are available to all programs automatically. Both are declared as final, so they never be sub classed.

2.15.1 String: It can be created through 3 constructors.

(i) String s=new String() means s is instance of string is created with no parameters.

(ii) String s=new String(Char[]) where a character array is initialized to string instance s.

Example is :

char c[]={‘a’,’b’,’c’};

String s=new String( c ); which initializes abc to string stance s.

(iii)String s=new String(char c[],int SI,int numchars) where numchars indicatenumber of characters to read from a character array from starting index SI in c[] .

Example is: char c[]={‘a’,’b’,’c’,’d’,’e’,’f’};

String s=new String(c,2,3}; produces cde

String also have constructors that take array of bytes as argument.

  1. String(byte ac[])

  2. String(byte ac[],int SI,int numchars)

There is a method length() that tells the number of characters in a string.

I. Character Extraction Methods: There are a number of methods to extract characters from a gven string.

(i) charAt(): it returns the character at the specified index or location in a given string. It’s general form is char charAt(int where) .

(ii) getChars(): It extracts the characters from a given string from SourceStsrt to SourceEnd and this is copied to target array from targetStart. It’s general from is

getChars(int SourceStart, int SourceEnd,char target[],int targetEnd).

  1. getBytes(): It stores that characters in an array of bytes. It’s general form is byte[] getBytes(); It can be used where the applications use 8 bit ASCII.

  2. toCharArray(): It converts all the characters in a given string into a character array.

II. String comparision methods: These are used to compare strings or substrings within strings.

  1. equals() & equalsIgnoreCase(): It returns true when the invoking string contain same characters as in argument string str. It’s general form is Boolean equals(String str) where as Boolean equalsIgnoreCase(String str) returns true if invoking string ignored case difference with argument string str and contain same characters in same order. It treats alphabets A-Z to be same as a-z.

  2. regionMatches(): It is used to compare a specific region in invoking string with another specific region in the another string. It’s first form is

a) boolean regionMatches(int StartIndex, String str2,int str2StartIndex,int numchars)

b) boolean regionMatches(boolean ignoreCase, int StartIndex, String str2,int str2StartIndex,int numchars)

In both cases, StartIndex specifies invoking string from starting index StartIndex is compared to str2 string from str2StartIndex and the number of characters are compared are indicated by numchars.

In second form, if ignoreCase is true, invoking string is compared with str2 by ignoring cases.

  1. startsWith() and endsWith():

  2. startsWith() returns true when invoking string begins with a specified string( argument string).It’s general form is boolean startsWith(String str)

The second form is boolean startsWith(String str, int StartIndex) searches from specified index from invoking string.

    1. endsWith() returns true when invoking string ends with specified string. It’s general form is boolean endsWith(String str)

  1. equals() versus ==: equals() compares all characters of two strings but == checks two object references refer to same instance.

  2. compareTo(): It returns 0 if invoking string and argument string are identical or less than 0 if the invoking string comes before the argument string in the dictionary order or greater than 0 if invoking string comes after the argument string in the dictionary order. It’s general form is int compareTo(String str)

III. Searching Strings: String class has two methods to search for specified character or substring in an invoking string.

  1. indexOf(): It searches for first occurrence of character or substring in a given string.

    1. int indexOf(char ch): gives the first occurrence of character in a given string.

    2. int indexOf(String str): gives the first occurrence of substring in a given string.

  1. lastIndexOF(): It searches for last occurrence of a character or substring in a given string.

    1. int lastindexOf(char ch): gives the last occurrence of character in a given string.

    2. int lastindexOf(String str): gives the ending occurrence of substring in a given string.

IV. String modification Methods: There are some important methods that alters the string and that can be stored in a new string.

  1. substring(): It takes part of given string. It’s forms are

    1. String substring(int startindex): it returns the substring from startingindex to last index in an invoking string.

    2. String subString(int startindex,int endindex): It gives a portion of a string from startindex to endindex-1.

An example is: s=”this a test”.

String s1=s.subString(1,4) returns his.

  1. concat(): It is used to concatenate two strings. It’s general from is String concat(String s)

Example is: String s1=”one”;

String s2=s1.concat(“two”); gives “onetwo” to s1.

  1. replace(): It replaces all occurrences of one character in a invoking string with another character. It’s general form is String replace(char original, char replacement)

Example is : String s=”Hello”.replace(‘l’,’w’) gives “hewwo” to s.

  1. trim(): It returns an absolute string by removing any leading and trailing white spaces in an invoking string. It’s general form is String trim()

Example is String s=” Hello World “.trim() gives “Hello World”.


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...