aspose file tools*
The moose likes Beginning Java and the fly likes characters and numbers seperation in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "characters and numbers seperation in a string" Watch "characters and numbers seperation in a string" New topic
Author

characters and numbers seperation in a string

praveen oruganti
Ranch Hand

Joined: Feb 05, 2007
Posts: 73
I am having string like "65234Markets6713Investments".

I need to seperate this string as 65234,Markets,6713,Investments.


I have used the logic like :



But i am able to get the ouput like 65234,Markets6713,Investments.

can any one help me out in this.



Regards,<br />Praveen Oruganti<br />Software Programmer
nek thomas
Greenhorn

Joined: Oct 18, 2008
Posts: 27
Praveen, you might wanna try this....

public static void main(String[] args) {
String s = "65234Markets6713Investments";
StringBuffer sb = new StringBuffer();
int pos = 1;
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {

if (Character.isDigit(ch[i])) {
if(pos!=1)
{
pos=1;
sb.append(',');
}
sb.append(ch[i]);

} else {
if (pos == 1) {
sb.append(',');
}
sb.append(ch[i]);
pos++;
}
}
System.out.println(sb.toString());
}




This gives this result

65234,Markets,6713,Investments



Nek
praveen oruganti
Ranch Hand

Joined: Feb 05, 2007
Posts: 73
Yes now it's working.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

You can try this


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

If we're going to throw around solutions, here's mine:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

its really beautiful Rob,
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Of course it is
But thanks for the compliment.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Since no one has posted the obligatory cryptic regex solution, I guess I'll provide one:



Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
manepalli rajesh
Greenhorn

Joined: Oct 19, 2008
Posts: 15
how to write the following condition can someone help me

1. the string should be like the following
You may use letters, numbers, underscores, and one dot (.) , i think with regular expressions we can do it.

2. one more string should be in the below format.
Use 6 to 32 characters, no spaces.

please somebody help me. this validation i am doing in serverside.




Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38793
    
  23
Please don't add a new question to somebody else's thread. That question probably merits a thread of its own, with a link to this thread if the patterns are similar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: characters and numbers seperation in a string