Unit:13 String handling

By Haitomns G

The String Constructors

The String class supports several constructors. To create an empty String, call the default constructor. For example,

String s = new String();

will create an instance of String with no characters in it.

Frequently, you will want to create strings that have initial values. The String class provides a variety of constructors to handle this. 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”.

You 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.

You 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. Consider this example:

// Construct one String from another. class MakeString {

  public static void main(String args[]) {     char c[] = {‘J’, ‘a’, ‘v’, ‘a’}; 

    String s1 = new String(c);

    String s2 = new String(s1);





The output from this program is as follows:

   Java    Java

As you can see, s1 and s2 contain the same string.

Even though Java’s char type uses 16 bits to represent the basic Unicode character set, the typical format for strings on the Internet uses arrays of 8-bit bytes constructed from the ASCII character set. Because 8-bit ASCII strings are common, the String class provides constructors that initialize a string when given a byte array. Two forms are shown here:

String(byte chrs[ ])

String(byte chrs[ ], int startIndex, int numChars)

Here, chrs specifies the array of bytes. The second form allows you to specify a subrange. In each of these constructors, the byte-to-character conversion is done by using the default character encoding of the platform. The following program illustrates these constructors:

// Construct string from subset of char array. class SubStringCons {

  public static void main(String args[]) {     byte ascii[] = {65, 66, 67, 68, 69, 70 };

    String s1 = new String(ascii);


    String s2 = new String(ascii, 2, 3);




This program generates the following output:



Extended versions of the byte-to-string constructors are also defined in which you can specify the character encoding that determines how bytes are converted to characters. However, you will often want to use the default encoding provided by the platform.

NOTE The contents of the array are copied whenever you create a String object from an array. If you modify the contents of the array after you have created the string, the String will be unchanged.

You can construct a String from a StringBuffer by using the constructor shown here: String(StringBuffer strBufObj)

You can construct a String from a StringBuilder by using this constructor:

String(StringBuilder strBuildObj)

The following constructor supports the extended Unicode character set:

String(int codePoints[ ], int startIndex, int numChars)

Here, codePoints is an array that contains Unicode code points. The resulting string is constructed from the range that begins at startIndex and runs for numChars. There are also constructors that let you specify a Charset.

NOTE A discussion of Unicode code points and how they are handled by Java is found in Chapter 17.

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);    System.out.println(s.length());

Special String Operations

Because strings are a common and important part of programming, Java has added special support for several string operations within the syntax of the language. These operations include the automatic creation of new String instances from string literals, concatenation of multiple String objects by use of the + operator, and the conversion of other data types to a string representation. There are explicit methods available to perform all of these functions, but Java does them automatically as a convenience for the programmer and to add clarity.

String Literals

The earlier examples showed how to explicitly create a String instance from an array of characters by using the new operator. However, there is an easier way to do this using a string literal. For each string literal in your program, Java automatically constructs a String object. Thus, you can use a string literal to initialize a String object. For example, the following code fragment creates two equivalent strings:

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

String s1 = new String(chars);

String s2 = “abc”; // use string literal

Because a String object is created for every string literal, you can use a string literal any place you can use a String object. For example, you can call methods directly on a quoted string as if it were an object reference, as the following statement shows. It calls the length( ) method on the string “abc”. As expected, it prints “3”.


String Concatenation

In general, Java does not allow operators to be applied to String objects. The one exception to this rule is the + operator, which concatenates two strings, producing a String object as the result. This allows you to chain together a series of + operations. For example, the following fragment concatenates three strings:

String age = “9”;

String s = “He is ” + age + ” years old.”;


This displays the string “He is 9 years old.”

One practical use of string concatenation is found when you are creating very long strings. Instead of letting long strings wrap around within your source code, you can break them into smaller pieces, using the + to concatenate them. Here is an example:

// Using concatenation to prevent long lines. class ConCat {

  public static void main(String args[]) {

    String longStr = “This could have been ” +

      “a very long line that would have ” +

      “wrapped around.  But string concatenation ” +

      “prevents this.”;




String Concatenation with Other Data Types

You can concatenate strings with other types of data. For example, consider this slightly different version of the earlier example:

int age = 9;

String s = “He is ” + age + ” years old.”;


In this case, age is an int rather than another String, but the output produced is the same as before. This is because the int value in age is automatically converted into its string representation within a String object. This string is then concatenated as before. The compiler will convert an operand to its string equivalent whenever the other operand of  the + is an instance of String.

Be careful when you mix other types of operations with string concatenation expressions, however. You might get surprising results. Consider the following:

String s = “four: ” + 2 + 2;


This fragment displays four: 22

rather than the four: 4

that you probably expected. Here’s why. Operator precedence causes the concatenation of “four” with the string equivalent of 2 to take place first. This result is then concatenated with the string equivalent of 2 a second time. To complete the integer addition first, you must use parentheses, like this:

String s = “four: ” + (2 + 2);

Now s contains the string “four: 4”.

String Conversion and toString( )

When Java converts data into its string representation during concatenation, it does so by calling one of the overloaded versions of the string conversion method valueOf( ) defined by String. valueOf( ) is overloaded for all the primitive types and for type Object. For the primitive types, valueOf( ) returns a string that contains the human-readable equivalent  of the value with which it is called. For objects, valueOf( ) calls the toString( ) method on the object. We will look more closely at valueOf( ) later in this chapter. Here, let’s examine  the toString( ) method, because it is the means by which you can determine the string representation for objects of classes that you create.

Every class implements toString( ) because it is defined by Object. However, the default implementation of toString( ) is seldom sufficient. For most important classes that you create, you will want to override toString( ) and provide your own string representations. Fortunately, this is easy to do. The toString( ) method has this general form: String toString( )

To implement toString( ), simply return a String object that contains the human-readable string that appropriately describes an object of your class.

By overriding toString( ) for classes that you create, you allow them to be fully integrated into Java’s programming environment. For example, they can be used in print( ) and println( ) statements and in concatenation expressions. The following program demonstrates this by overriding toString( ) for the Box class:

// Override toString() for Box class. class Box {   double width;   double height;   double depth;

  Box(double w, double h, double d) {

    width = w;     height = h;     depth = d;


  public String toString() {

    return “Dimensions are ” + width + ” by ” +

            depth + ” by ” + height + “.”;


class toStringDemo {

  public static void main(String args[]) {

    Box b = new Box(10, 12, 14);

    String s = “Box b: ” + b; // concatenate Box object 

    System.out.println(b); // convert Box to string




The output of this program is shown here:

   Dimensions are 10.0 by 14.0 by 12.0

   Box b: Dimensions are 10.0 by 14.0 by 12.0

As you can see, Box’s toString( ) method is automatically invoked when a Box object is used in a concatenation expression or in a call to println( ).

Character Extraction

The String class provides a number of ways in which characters can be extracted from a String object. Several are 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 nonnegative 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.

The following program demonstrates getChars( ):

class getCharsDemo {

  public static void main(String args[]) {

    String s = “This is a demo of the getChars method.”;

    int start = 10;     int end = 14;

    char buf[] = new char[end – start];

    s.getChars(start, end, buf, 0);




Here is the output of this program:    demo getBytes( )

There is an alternative to getChars( ) that stores the characters in an array of bytes. This method is called getBytes( ), and it uses the default character-to-byte conversions provided by the platform. Here is its simplest form:

byte[ ] getBytes( )

Other forms of getBytes( ) are also available. getBytes( ) is most useful when you are exporting a String value into an environment that does not support 16-bit Unicode characters. For example, most Internet protocols and text file formats use 8-bit ASCII for all text interchange.

toCharArray( )

If you want to convert all the characters in a String object into a character array, the easiest way is to call toCharArray( ). It returns an array of characters for the entire string. It has this general form:

char[ ] toCharArray( )

This function is provided as a convenience, since it is possible to use getChars( ) to achieve the same result.

String Comparison

The String class includes a number of methods that compare strings or substrings within strings. Several are examined here.

equals( ) and equalsIgnoreCase( )

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 case-sensitive.

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( ):

// Demonstrate equals() and equalsIgnoreCase(). class equalsDemo {

  public static void main(String args[]) {

    String s1 = “Hello”;

    String s2 = “Hello”;

    String s3 = “Good-bye”;

    String s4 = “HELLO”;

    System.out.println(s1 + ” equals ” + s2 + ” -> ” +


    System.out.println(s1 + ” equals ” + s3 + ” -> ” +


    System.out.println(s1 + ” equals ” + s4 + ” -> ” +


    System.out.println(s1 + ” equalsIgnoreCase ” + s4 + ” -> ” +




The output from the program is shown here:

   Hello equals Hello -> true

   Hello equals Good-bye -> false

   Hello equals HELLO -> false

   Hello equalsIgnoreCase HELLO -> true

regionMatches( )

The regionMatches( ) method compares a specific region inside a string with another specific region in another string. There is an overloaded form that allows you to ignore  case in such comparisons. Here are the general forms for these two methods:

boolean regionMatches(int startIndex, String str2,

                                        int str2StartIndex, int numChars)

boolean regionMatches(boolean ignoreCase,

                                        int startIndex, String str2,                                         int str2StartIndex, int numChars)

For both versions, startIndex specifies the index at which the region begins within the invoking String object. The String being compared is specified by str2. The index at which the comparison will start within str2 is specified by str2StartIndex. The length of the substring being compared is passed in numChars. In the second version, if ignoreCase is true, the case of the characters is ignored. Otherwise, case is significant.

startsWith( ) and endsWith( )

String defines two methods that are, more or less, specialized forms of regionMatches( ).

The startsWith( ) method determines whether a given String begins with a specified string. Conversely, endsWith( ) determines whether the String in question ends with a specified string. They have the following general forms:

boolean startsWith(String str) boolean endsWith(String str)

Here, str is the String being tested. If the string matches, true is returned. Otherwise, false is returned. For example,

“Foobar”.endsWith(“bar”) and

“Foobar”.startsWith(“Foo”) are both true.

A second form of startsWith( ), shown here, lets you specify a starting point: boolean startsWith(String str, int startIndex)

Here, startIndex specifies the index into the invoking string at which point the search will begin. For example,

“Foobar”.startsWith(“bar”, 3) returns true.

equals( ) Versus ==

It is important to understand that the equals( ) method and the == operator perform two different operations. As just explained, the equals( ) method compares the characters inside a String object. The == operator compares two object references to see whether they refer to the same instance. The following program shows how two different String objects can contain the same characters, but references to these objects will not compare as equal:

// equals() vs == class EqualsNotEqualTo {

  public static void main(String args[]) {

    String s1 = “Hello”;

    String s2 = new String(s1);

    System.out.println(s1 + ” equals ” + s2 + ” -> ” +


    System.out.println(s1 + ” == ” + s2 + ” -> ” + (s1 == s2));



The variable s1 refers to the String instance created by “Hello”. The object referred  to by s2 is created with s1 as an initializer. Thus, the contents of the two String objects are identical, but they are distinct objects. This means that s1 and s2 do not refer to the same objects and are, therefore, not ==, as is shown here by the output of the preceding example:

   Hello equals Hello -> true    Hello == Hello -> false

compareTo( )

Often, it is not enough to simply know whether two strings are identical. For sorting applications, you need to know which is less than, equal to, or greater than the next. A string  is less than another if it comes before the other in dictionary order. A string is greater than another if it comes after the other in dictionary order. The method compareTo( ) serves this purpose. It is specified by the Comparable<T> interface, which String implements. It has this general form:

int compareTo(String str)

Here, str is the String being compared with the invoking String. The result of the comparison is returned and is interpreted as shown here:

Less than zeroThe invoking string is less than str.
Greater than zeroThe invoking string is greater than str.
ZeroThe two strings are equal.

Here is a sample program that sorts an array of strings. The program uses compareTo( ) to determine sort ordering for a bubble sort:

// A bubble sort for Strings. class SortString {   static String arr[] = {

    “Now”, “is”, “the”, “time”, “for”, “all”, “good”, “men”,

    “to”, “come”, “to”, “the”, “aid”, “of”, “their”, “country”


  public static void main(String args[]) {     for(int j = 0; j < arr.length; j++) {       for(int i = j + 1; i < arr.length; i++) {         if(arr[i].compareTo(arr[j]) < 0) {

          String t = arr[j];

          arr[j] = arr[i];           arr[i] = t;


      }       System.out.println(arr[j]);


  } }

The output of this program is the list of words:

   Now    aid    all    come    country    for    good    is    men    of    the    the    their    time    to    to

As you can see from the output of this example, compareTo( ) takes into account uppercase and lowercase letters. The word “Now” came out before all the others because it begins with an uppercase letter, which means it has a lower value in the ASCII character set.

If you want to ignore case differences when comparing two strings, use compareToIgnoreCase( ), as shown here:

int compareToIgnoreCase(String str)

This method returns the same results as compareTo( ), except that case differences are ignored. You might want to try substituting it into the previous program. After doing so, “Now” will no longer be first.

Modifying a String

Because String objects are immutable, whenever you want to modify a String, you must either copy it into a StringBuffer or StringBuilder, or use a String method that constructs a new copy of the string with your modifications complete. A sampling of these methods are described here.

substring( )

You 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.

The following program uses substring( ) to replace all instances of one substring with another within a string:

// Substring replacement. class StringReplace {

  public static void main(String args[]) {

    String org = “This is a test. This is, too.”;

    String search = “is”;

    String sub = “was”;     String result = “”;

    int i;

    do { // replace all matching substrings

      System.out.println(org);       i = org.indexOf(search);

      if(i != -1) {

        result = org.substring(0, i);         result = result + sub;

        result = result + org.substring(i + search.length());

        org = result;


    } while(i != -1);



The output from this program is shown here:

   This is a test. This is, too.

   Thwas is a test. This is, too.

   Thwas was a test. This is, too.

   Thwas was a test. Thwas is, too.    Thwas was a test. Thwas was, too.

concat( )

You can concatenate two strings using concat( ), shown here:

String concat(String str)

This method creates a new object that contains the invoking string with the contents of str appended to the end. concat( ) performs the same function as +. For example,

String s1 = “one”;

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

puts the string “onetwo” into s2. It generates the same result as the following sequence:

String s1 = “one”; String s2 = s1 + “two”;

replace( )

The replace( ) method has two forms. The first replaces all occurrences of one character in the invoking string with another character. It has the following general form: String replace(char original, char replacement)

Here, original specifies the character to be replaced by the character specified by replacement. The resulting string is returned. For example,

String s = “Hello”.replace(‘l’, ‘w’); puts the string “Hewwo” into s.

The second form of replace( ) replaces one character sequence with another. It has this general form:

String replace(CharSequence original, CharSequence replacement)


trim( )

The trim( ) method returns a copy of the invoking string from which any leading and trailing whitespace has been removed. It has this general form: String trim( )

Here is an example:

String s = ”   Hello World    “.trim();

This puts the string “Hello World” into s.

The trim( ) method is quite useful when you process user commands. For example,  the following program prompts the user for the name of a state and then displays that state’s capital. It uses trim( ) to remove any leading or trailing whitespace that may have inadvertently been entered by the user.

// Using trim() to process commands. import java.io.*;

class UseTrim {

  public static void main(String args[])

    throws IOException


    // create a BufferedReader using System.in

    BufferedReader br = new

      BufferedReader(new InputStreamReader(System.in));

    String str;

    System.out.println(“Enter ‘stop’ to quit.”);

    System.out.println(“Enter State: “);

    do {

      str = br.readLine();

      str = str.trim(); // remove whitespace


        System.out.println(“Capital is Springfield.”);

      else if(str.equals(“Missouri”))

        System.out.println(“Capital is Jefferson City.”);

      else if(str.equals(“California”))

        System.out.println(“Capital is Sacramento.”);

      else if(str.equals(“Washington”))         System.out.println(“Capital is Olympia.”);       // …

    } while(!str.equals(“stop”));



Also, Read

  1. Unit: 7 A Closer Look at Methods and Classes
  2. Unit: 8 Inheritance
  3. Unit: 9 Packages and Interface
  4. Unit: 10 Exception Handling
  5. Unit-11 Multithreaded Programming
  6. Unit:12 Enumerations, autoboxing, and annotations

Haitomns G. is a desktop, android, and web developer based in Nepal. He has worked on building several websites, apps, and softwares for clients and independent projects. He is experienced in C, C++, C#, Java, Python, SQL, HTML, CSS, PHP, and JavaScript.

1 thought on “Unit:13 String handling”

  1. What i don’t understood is in truth how you’re not really much more well-favored than you might be now. You are so intelligent. You realize therefore considerably in the case of this subject, produced me for my part imagine it from so many numerous angles. Its like women and men don’t seem to be fascinated unless it’s one thing to do with Girl gaga! Your own stuffs nice. All the time take care of it up!


Leave a Comment

Slide to prove you're not a bot/spammer *