Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

characters and numbers seperation in a string

 
praveen oruganti
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
nek thomas
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes now it's working.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try this


 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we're going to throw around solutions, here's mine:
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its really beautiful Rob,
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it is
But thanks for the compliment.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since no one has posted the obligatory cryptic regex solution, I guess I'll provide one:

 
manepalli rajesh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic