• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How appened different character in to a string

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I hava a keyword "subodh2000". I want to cut the numeric from it.
From following code i have get numeric character now i m facing problem to appened those character.

for ( int i = 0; i < message.length(); i++ ) {
char c = message.charAt(i);
int j = (int) c;
if(j>=48 && j<=57)
{
String aChar = new Character(c).toString();

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

Here is your solution.

String str = "subodh2000";
StringBuffer strNum = new StringBuffer("");
StringBuffer strChar = new StringBuffer("");
for ( int i = 0; i < str.length(); i++ )
{
char c = str.charAt(i);
int j = (int) c;
String aChar = new Character(c).toString();
if(j>=48 && j<=57)
{
strNum.append(aChar);
}
else
{
strChar.append(aChar);
}
}

Explanation: -
---------------
You just have to go one step extra and split the string. If the current character is a number, append it to a StringBuffer (strNum). otherwise, append that character to another StringBuffer (strChar).

Hope you have understood this.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If all you need is a String with the numbers removed, you can use this...

Do you need to do something else with it?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:
  • Create 3 new instances of StringBuffer. One for letters, one for numbers, and one for "other."
  • Loop through your original String one char at a time.
  • Use Character methods isLetter(char c) and isDigit(char c) to categorize each char.
  • Append the char to the appropriate StringBuffer.
  • When finished looping, convert the StringBuffers to Strings.
  •  
    Dharmesh Gangani
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Mark webber's first suggestion would be the best if you want to remove numbers from the string. i.e. you just want the non-numeric part from the given string.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic