Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parsing various strings

Status
Not open for further replies.

sandeepmur

Programmer
Dec 14, 2003
295
PT
Hi,

I have a method which receives 5 Strings and each of the string may have the characters ";" and "\n".

I need to parse each of the strings and remove these characters before they are written to a file. I tried using a StringTokenizer which works effectively on any given string and for any character but am so far unable to get to a soln for parsing all the strings for all the characters as soon as the method is called.

Any suggestions ?
thnx
 
String class has a replace function to replace any char with another.

Here are the docs:

public String replace(char oldChar,
char newChar)

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.

Examples:

"mesquite in your cellar".replace('e', 'o')
returns "mosquito in your collar"
"the war of baronets".replace('r', 'y')
returns "the way of bayonets"
"sparring with a purple porpoise".replace('p', 't')
returns "starring with a turtle tortoise"
"JonL".replace('q', 'x') returns "JonL" (no change)

Parameters:
oldChar - the old character.
newChar - the new character.
Returns:
a string derived from this string by replacing every occurrence of oldChar with newChar.

Ed
 
While he want's to remove characters, your hints show how to replace characters.
Will it work with replace ('\n', null)?
Well - instead of asking I could just test it.
Being to lazy, I show an alternate approach, from which I know it will work:
Code:
target = source.replaceAll ("[\n;]", "");
(which uses regular expressions - you may believe me, or have a look at regexp - in the latter case take some time with you. :)

To call it for 5 strings, I can appreciate the for-loop.

seeking a job as java-programmer in Berlin:
 
You will have to walk through the whole string and remove any unwanted characters. Here is what I would do:

Code:
public String removeUnwantedChar(String strContainsUnwantedChar) {

   StringBuffer buffer = new StringBuffer();
   for (int i=0; i<strContainsUnwantedChar.lengh(); i++) {
     char c = strContainsUnwantedChar.charAt(i);
     switch(c) {
       case '\n':
       case ';': break;
       default: buffer.append(c);
     }
   }
   return buffer.toString();
}
 
as a alternative for Java 1.4+

you can use replaceAll()
example:
Code:
String test = "abc;efg\nijk";
String filteredStr = test.replaceAll(";", "");
filteredStr = filteredStr.replaceAll("\n", "");

The drawback of this is it will have to walk through the string multiple times.
 
With the regular expression showed above, you call the method once for both, ';' and '\n' and don't need any hand-iteration.
Code:
target = "this;;\n is\nno,;test;;\n\n".replaceAll ("[\n;]", "");


seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top