• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

character representation of a blank space

 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

what is the character representation of a blank space?
Iam trying to remove commas from a String
for ex:
String oldString="2,8764";
I can replace using String.replace(String,String) method.
but our client uses jdk1.3. So i need to use String.replace(char,char) method.

The resulting string should be like this "28764" (note:there should not be any space between).

Thanks,
Vinod
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vinod Awar:
Hi all,

what is the character representation of a blank space?
Iam trying to remove commas from a String
for ex:
String oldString="2,8764";
I can replace using String.replace(String,String) method.
but our client uses jdk1.3. So i need to use String.replace(char,char) method.

The resulting string should be like this "28764" (note:there should not be any space between).

Thanks,
Vinod



This link will answer your question.
 
Vinod Awar
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have read the documentation. There is only one method for replacing.
i.e replace(char,char). How can i get the same result as this:
String oldStr="2,756";
oldStr=oldStr.replaceAll(",","");

using the replace(char,char) method of java 1.3

Thanks,
Vinod
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try replace(',', '')
 
Vinod Awar
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Try replace(',', '')



gives compilation error: Invalid Character constant
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vinod Awar:


gives compilation error: Invalid Character constant



you have to try



Here \u0020 represent space
 
Vinod Awar
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


replaceAll(",","\u0020")


But it replaces "," with a space.

it is not the same as replaceAll()..

output:
str:#BC 2,84,56
str:#BC 2 84 56
str:#BC 28456

See the third line there is no space between numbers.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai pal,
Now i got confused with your requirement

Please let me get your requirements correct,you need all "," in the String to be replaced or removed?

My assumption was that

Source String: "2,8456"
Final String: "2 8456" (this is just replacement)

If you want to remove the "," from your string like this following case

Source String: "2,8456"
Final String: "28456" (this is removal)

then your code should be like this

[ January 31, 2008: Message edited by: Balasubramanian Chandrasekaran ]
 
Vinod Awar
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Balasubramanian
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you could use a StringBuffer - it's charAt() and deleteCharAt() methods could be used in a loop to do what you need. This has the advantage of not creating lots of String objects (each call to substring and concat will create a new String), although for short Strings with few commas, it's probably not that much of a problem.

If you use Balasubramanian's code you will need to change the last but one line to
s = s.trim();
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic