04-14-2010 10:12 AM
String array={"aaaa","bbb","cc*%&vvc"};
how will remove the *%& from the required above string.
Please help with a piece of code.
Thanks in advance.
04-14-2010 10:15 AM
04-14-2010 10:39 AM
Can you provide a code for this..
Thanks in advance
04-14-2010 10:41 AM
04-14-2010 01:12 PM
Your requirement isn't very well defined. For instance, this will work:
array[2] = "ccvvc";
but I suspect that you are looking to solve a more general problem. But what problem? If you want to remove all occurences of the characters '*', '%', or '&', then take a look at StringUtilities.removeChars. If you want to remove the character sequence "*%&", then using String.indexOf and substring as Simon suggests is the way to go. However, you need to define what should happen if you start with something like "cc*%&vvc*%&vvc" (which one(s) to remove?). Or even "cc**%&%&vvc" (there's only one occurrence of "*%&", but after it's removed, all of a sudden there's another one!)
04-14-2010 02:14 PM
I think Simon's point is valid. This is a Java question and would perhaps be better directed at a forum that is specifically for Java. In addition, if someone can't create a piece of Java code that uses substring and indexOf, then perhaps a course is needed.
04-15-2010 09:53 AM
I'm afraid I don't have any links for Java newbies at the moment. But if you Google "Introduction to Java" I'm sure there are a few good online tutorials out there (Who reads these days?
)
Good luck,
~Dom
PS - String array={"aaaa","bbb","cc*%&vvc"}; is improper syntax --
You would get a compiler error because you forgot to include the square brackets ('[' and ']') to indicate that it is an array. Try:
String [] array = { "aaaa", "bbb", "cc*%&vvc" };
Then, to remove '*%&'...
String s = array [2];
String s2 = *%&;
s = s.substring (0, s.indexOf (s2)) //"cc"
s += s.substring (s.indexOf (s2) + s2.Length, s.Length () - (s.indexOf (s2) + s2.Length ())); //"cc" + "vvc"
array [2] = s;
That's a pretty ugly snippet, but basically what it's doing is removing the unwanted segment by creating a new string which is the first segment, "cc", plus the end segment, "vvc".