Unit 13 : String handling

3rd Semester

 A string is a set of one or more characters enclosed in double quotes or simply a string is an array of characters.
 Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.
 The Java platform provides the String class to create and manipulate strings.

String Constructor

The String class supports several constructors as follows:
1. To create an empty String, we call the default constructor.
For example,
String s = new String(); will create an instance of String with no characters in it.
2. To create a String initialized by an array of characters, use the constructor shown here:
String(char chars[ ])
Here is an example:
char chars[] = { ‘a’, ‘b’, ‘c’ };
String s = new String(chars);
This constructor initializes s with the string “abc”.
3. We can specify a subrange of a character array as an initializer using the following constructor:
String(char chars[ ], int startIndex, int numChars)
Here, startIndex specifies the index at which the subrange begins, and numChars specifies the number of characters to use. Here is an example:
char chars[] = { ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’ };
String s = new String(chars, 2, 3);
This initializes s with the characters cde.
4. We can construct a String object that contains the same character sequence as another String object
using this constructor:
String(String strObj)
Here, strObj is a String object.

String initialization using byte array.


String Length

The length of a string is the number of characters that it contains. To obtain this value, call the length( )
method, shown here:
int length( )
The following fragment prints “3”, since there are three characters in the string s:
char chars[] = { ‘a’, ‘b’, ‘c’ };
String s = new String(chars);

Character Extraction

The String class provides a number of ways in which characters can be extracted from a String object.Each is examined here. Although the characters that comprise a string within a String object cannot be indexed as if they were a character array, many of the String methods employ an index (or offset) into  the string for their operation. Like arrays, the string indexes begin at zero.
charAt( )

To extract a single character from a String, you can refer directly to an individual character via the charAt( ) method. It has this general form:
char charAt(int where)
Here, where is the index of the character that you want to obtain. The value of where must be non negative and specify a location within the string. charAt( ) returns the character at the specified
location. For example,
char ch;
ch = “abc”.charAt(1);
assigns the value “b” to ch.
getChars( )
If you need to extract more than one character at a time, you can use the getChars( ) method. It has
this general form:
void getChars(int sourceStart, int sourceEnd, char target[ ], int targetStart) Here, sourceStart specifies the index of the beginning of the substring, and sourceEnd specifies an index that is one past the end of the desired substring. Thus, the substring contains the characters from sourceStart through sourceEnd–1. The array that will receive the characters is specified by target. The index within target at which the substring will be copied is passed in targetStart. Care must be taken to assure that the target
array is large enough to hold the number of characters in the specified substring.

Demonstration of getChars( ).


String Comparisons

To compare two strings for equality, use equals( ). It has this general form:
boolean equals(Object str)
Here, str is the String object being compared with the invoking String object. It returns true if the strings contain the same characters in the same order, and false otherwise. The comparison is casesensitive.To perform a comparison that ignores case differences, call equalsIgnoreCase( ). When it compares two strings, it considers A-Z to be the same as a-z. It has this general form:
boolean equalsIgnoreCase(String str)
Here, str is the String object being compared with the invoking String object. It, too, returns true if the strings contain the same characters in the same order, and false otherwise.
Here is an example that demonstrates equals( ) and equalsIgnoreCase( ):
Demonstration of String comparisons.

Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true

Modifying a String

Because String objects are immutable, whenever we want to modify a String, we must either copy it into a StringBuffer or use one of the following String methods, which will construct a new copy of the string with your modifications complete. We can extract a substring using substring( ). It has two forms:
The first is String substring(int startIndex).Here, startIndex specifies the index at which the substring will begin. This form returns a copy of the substring that begins at startIndex and runs to the end of the
invoking string.
The second form of substring( ) allows you to specify both the beginning and ending index of the substring:String substring(int startIndex, int endIndex). Here, startIndex specifies the beginning index,  and endIndex specifies the stopping point. The string returned contains all the characters from the beginning index, up to, but not including, the ending index.
Demonstration of string modification.