File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How appened different character in to a string

 
subodh varshney
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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();

}
 
Dharmesh Gangani
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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
    • 0
    • Mark post as helpful
    • send pies
    • 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic