concat(), replace(), and trim() Strings in Java

By: Ivan Lim Emailed: 1730 times Printed: 2344 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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

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
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"));
}
}

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(3)


1. View Comment

one of the best example for trim()....

View Tutorial          By: sharad at 2013-01-29 13:43:58
2. View Comment

Can I ask something,

Can you show me how to use TRIM in GUI

TQ =)


View Tutorial          By: wawa at 2014-05-03 15:28:35
3. View Comment

how to trim in jsp page and to send the trimed conten to servlet?

View Tutorial          By: monishaa at 2015-07-16 10:06:11

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Ivan Lim
Requesting a Signed Certificate from a Certification Authority example using keytool in Java
The if-then Statement in Java
SELECT Statements
The BodyTag Interface in JSP
Handling Duplicate Form Submissions in Struts
Standard Input and Output in C
A sample that shows Java Beans, Servlets and JSP working together
Recursion in C
Using Multibox in Struts
switch in C
Word Counting sample program in C
Java Bean Scopes in JSF
Using cout.width() in C++
Types of configurations in J2ME
How to get the CLDC and MIDP version from a J2ME program

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
Student marks calculation program in Java
Count number of vowels, consonants and digits in a String in Java
XML and Java - Parsing XML using Java Tutorial
What is Java?
FileReader and FileWriter example program in Java
Stack example in Java - push(), pop(), empty(), search()
Method Overriding in Java
arraycopy example in Java
Read from a COM port using Java program
How to use ArrayList in Java
Method Overloading (function overloading) in Java
InetAddress Example program in Java
The Basic Structure of a Simple Java program
The switch Statement example in Java
Most Emailed Articles (in Java)
The Unsigned Right Shift
Reverse a number in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to display numbers from 1 to 10 on Applet
Insert, Delete, Search, Print an int Array in Java
Steps in using verisign certificate with Glassfish appserver
Step by Step guide to setup freetts for Java
Method Overloading sample in Java
Method Override sample in Java
How Java is Organized?
Setting Up SSL on Tomcat
Compare compareTo() function of String class with Collator and CollationKey
Applet Notinited : Applet xxxxxxxxxxxx notinited
The break Statement example in Java