Home | | Web Programming | Additional String Methods

Chapter: Java The Complete Reference : The Java Library : String Handling

Additional String Methods

In addition to those methods discussed earlier, String has many other methods, including those summarized in the following table:

Additional String Methods

 

In addition to those methods discussed earlier, String has many other methods, including those summarized in the following table:







Joining Strings

 

JDK 8 adds a new method to String called join( ). It is used to concatenate two or more strings, separating each string with a delimiter, such as a space or a comma. It has two forms. Its first is shown here:

 

static String join(CharSequence delim, CharSequence . . . strs)

 

Here, delim specifies the delimiter used to separate the character sequences specified by strs. Because String implements the CharSequence interface, strs can be a list of strings. (See Chapter 17 for information on CharSequence.) The following program demonstrates this version of join( ):

 

 

// Demonstrate the join() method defined by String.

class StringJoinDemo {

 

public static void main(String args[]) {

 

String result = String.join(" ", "Alpha", "Beta", "Gamma");

 

System.out.println(result);

 

result = String.join(", ", "John", "ID#: 569", "E-mail: John@HerbSchildt.com");

 

System.out.println(result);

 

}

 

}

 

The output is shown here:

 

Alpha Beta Gamma

 

John, ID#: 569, E-mail: John@HerbSchildt.com

In the first call to join( ), a space is inserted between each string. In the second call, the delimiter is a comma followed by a space. This illustrates that the delimiter need not be just a single character.

The second form of join( ) lets you join a list of strings obtained from an object that implements the Iterable interface. Iterable is implemented by the Collections Framework classes described in Chapter 18, among others. See Chapter 17 for information on Iterable.

 


 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Java The Complete Reference : The Java Library : String Handling : Additional String Methods |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.