File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How appened different character in to a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How appened different character in to a string" Watch "How appened different character in to a string" New topic
Author

How appened different character in to a string

subodh varshney
Greenhorn

Joined: Aug 01, 2005
Posts: 11
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

Joined: Feb 20, 2004
Posts: 30
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.


-=-=-=-=-=-=-=-=-=-<br />Thanks & Regards,<br />Dharmesh G.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If all you need is a String with the numbers removed, you can use this...

Do you need to do something else with it?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

    Joined: Feb 20, 2004
    Posts: 30
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How appened different character in to a string