Removing String Spaces in JAVA Programming

Asked By 10 points N/A Posted on -
qa-featured

Good day guys!

I am a Visual Basic Programmer and knows a little Java programming too.

When it comes to VB all I want to do to remove the spaces is to use the function Trim().

But in Java programming I don't really have an idea on how to do it.

What is the method or code to remove spaces in String values?

SHARE
Answered By 0 points N/A #99056

Removing String Spaces in JAVA Programming

qa-featured

String is a datatype used extensively in JAVA. We can replace the characters in the string with a predefined string.

The format is 
String str;
str.RelpaceAll(“ ”,””);
system.out.println ( str ) ;
Other way is to search each location and do the rest.
String str;
for(i=0;i<str.length-1;i++){
 if str.charAt(i)==” “ {
  str.charAt(i)=””;
}
You have asked about replacing the white spaces with nothing, however, the TRIM keyword replaces only the leading and ending white spaces. However for such work of TRIM, you do this,
str.trim();
 
Here the trim function is at the string class which removes the leading and trailing white spaces.
Wish you best of luck. Please let me know it worked.
 
TRIM appears in Visual Basic, SQL, PHP and in many other programming languages too.
 
Thanks.

Related Questions