• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validating a String

 
sandy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have a string :
A&B!!C&&&D!&E
where in the alpabets are different Strings. Now, the validation of this String must result into :
A&B!C&D!E
i.e the symbols & and ! in between the Strings should'nt be repeated one after the other. In other words, there should be only one symbol either & or ! between 2 strings. How can I go this validation ?
:roll:
 
keerthi matta
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sandy,
I think the below code does solve ur problem,
public StringTest()
{
String testStr = new String();
testStr = "A&B!!C&&&D!&E";
int len = testStr.length();
String resultStr = new String();
for (int i=0;i<len;i++)
{
if(testStr.charAt(i)=='!'||testStr.charAt(i) == '&')
{
if (!(resultStr.endsWith("!")||resultStr.endsWith("&")))
{
resultStr = resultStr+testStr.charAt(i);
}
}
else
{
resultStr = resultStr+testStr.charAt(i);
}
}
System.out.println("Resultant Str is "+resultStr);
}
hope this solves ur requirement
njoy coding
bye
keerthi
 
sandy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Keerthi
Thanks a lot for the immediate reply. Your answer did solve my problem. I was trying the harder way by using StringTokenizing. Thanks again !
Sandy
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"sandy" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!