Modifying a String

  • As String objects are immutable to modify a string we have to use either StringBuffer or StringBuilder or we can use a String method that constructs a new copy of the string with modifications complete.

substring( )

  • We can extract a substring using substring( ).It has two forms

String substring(int startIndex)

  • startIndex specifies the index at which the substring will begin.
  • It returns a copy of the substring that begins at startIndex and runs to the end of the invoking string.

String substring(int startIndex, int endIndex)

  • It allows to specify both start and end indexes to create a substring.
  • startIndex specifies the beginning index, and endIndex specifies the stopping point.
// Substring replacement.
public 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);
 }
}

Output:

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

  • We can concatenate two strings using concat() method with the following general form:

String concat(String str)

  • str is the String to concatenate with the invoking String.
  • This will return a String object after concatenation with the invoking string object.
//concatenation example
public class ConcatenationDemo
{
  public static void main(String args[])
  {
    String str1= "Hello";
    String str2 = "Hi";

    String result1 = str1.concat("World");
    String result2 = result1.concat(str2);

    System.out.println("concatenation of str1 and /World/ string::"+result1);
    System.out.println("concatenation of result1 and str1::"+result2);

  }
}

Output:

concatenation of str1 and /World/ string::HelloWorld
concatenation of result1 and str1::HelloWorldHi

replace( )

  • This method have two forms:

  • firstreplaces all occurrences of one characte rin the invoking string with another character by using following general form.

String replace(char original, char replacement)

  • original specifies the character to be replaced by the character specified by replacement.
  • The resulting string is returned.

  • The second form of replace( ) replaces one character sequence with another by using the following general form:

String replace(CharSequence original, CharSequence replacement)

//replace example
public class ReplaceDemo
{
  public static void main(String args[])
  {
    String str1 = "Hello World";
    String str2 = "this is test,this is too";

    String result1= str1.replace('o','z');
    String result2= str2.replace("is","this");
    System.out.println("Replacing o with z in Hello World::"+result1);
    System.out.println("Replacing sequence 'is' with 'this' in this is test,this is too::"+result2);

  }
}

Output:

Replacing o with z in Hello World::Hellz Wzrld
Replacing sequence 'is' with 'this' in this is test,this is too::ththis this test,ththis this too

trim( )

  • It returns a copy of the invoking string from which any leading and trailing whitespace has been removed by using following general form:

String trim( )

  • It is useful when we process user commands.
// Using trim() to process commands.
import java.io.*;
public 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;
 String str1 = "    Hello    ";
 System.out.println("using trim()::"+str1.trim());
 System.out.println("Enter 'stop' to quit.");
 System.out.println("Enter State: ");
 do {
 str = br.readLine();
 str = str.trim(); // remove whitespace
 if(str.equals("Illinois"))
 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"));
 }
}

Input:

Missouri

                **   stop**

Output:

using trim()::Hello

Enter 'stop' to quit.

Enter State:

Capital is Jefferson City.

results matching ""

    No results matching ""