This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

spaces in String

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I get rid of spaces(if any), in a String that I am manipualtng?
For ex:
String s = "some String";
Before I proceed to use this String, if I want to get rid of the space between 'some' and 'String', what's the easiest way of doing it? Thanks for your help.
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if Java has a function that does that...At work, so I don't have my Java books handy. You can loop through the length of the string, using the charAt() method to see if it's spaces. And then move the characters you need into another variable.
Good luck
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this:
<PRE>
StringBuffer sb;
String s;
s = s.trim();
sb = new StringBuffer(s);
StringTokenizer st = new StringTokenizer(s);
while(st.hasMoreTokens()){
sb.append(st.nextToken());
}
s = sb.toString();
</PRE>
 
Asuthosh Borikar
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bodie! That's what I had in mind too, but wanted to know if there's a better way of doing this.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this if you want to trim the string given on command line
import java.io.*;
class UseTrim{
public static void main(String args[])throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
do{
str = br.readLine();
str = str.trim();
System.out.println(str);
}while(str == Null);
}
}
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I did ...
The trim() method does away with trailing and leading spaces but not the in-between spaces. I think this is better than using IO

public class Test {
public static void main(String args[]) {
StringBuffer s1 = new StringBuffer("Hello u all");
StringBuffer s2 = new StringBuffer();
s2.ensureCapacity(s1.length());
for(int i=0;i<s1.length();i++) {>
char c = s1.charAt(i);
try {
if(c != ' '){
s2.append(c);
}
}catch(StringIndexOutOfBoundsException e) {
System.out.println(c);
System.out.println("stringerror " + e);
}
}
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!