The Artima Developer Community
Sponsored Link

Java Answers Forum
Isolate/remove parts of a String

6 replies on 1 page. Most recent reply: Apr 8, 2005 10:13 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
Shaitan

Posts: 11
Nickname: shaitan00
Registered: Feb, 2005

Isolate/remove parts of a String Posted: Apr 5, 2005 10:12 PM
Reply to this message Reply
Advertisement
Coding Style: NetBeans IDE 4.0 Beta2 (Java)

I need to find a good way to remove and isolate a part of a string, for example find the string below:
String = "0,1,3,6";

I need to find a way to isolate any given value (0, 1, 3, or 6) and remove it, so for example assume I want to remove "3" the string would then become = "0,1,6"
Also, I need to be able to remove from the head and tail of the string, like remove the "0" or "6".

Is there a good way to do this? Using .IndexOf and .SubString there seems to be a way (find the index right before and the index +2 to get to the next character) but I that will have issues at the head and tail, thought there might be a better way.
Any clues?


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Isolate/remove parts of a String Posted: Apr 6, 2005 4:21 AM
Reply to this message Reply
There are 2 "stylisher" ways to do it:

1.
String s = "0,1,3,6";
s = s.replace(",3,", ",");

This works of course only if the number is not at the beginning or end of the set

2.
String s = "0,1,3,6";
String[] nums = s.split(",");
String s2 = "";
for (int i = 0; i < nums.length; i++)
  if (!"3".equals(nums[i])
    s2 += "," + nums[i];
if (s2.length() > 0)
  s2 = s2.substring(1);

This way you can check more numbers at once, if you change the line wich contains "3".equals

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Isolate/remove parts of a String Posted: Apr 6, 2005 4:31 AM
Reply to this message Reply
To method 1:
You could bee naive enough to try replace("3", ""), but that ould cause problems with numbers wich have 2 or more digits.

You could do the following:

if s.startsWith("3")
  s = s.substring ("3".length());
if (s.length > 0)
  s = s.substring(1);
 
if s.endsWith("3")
  s = s.substring (s.length() - "3".length());
if (s.length > 0)
  s = s.substring(0, s.length()-1);
 
s = s.replace(",3,", ",");


This method will eliminate up to 3 "3"'s

If you want to get rid of any "3" in your String, you must change the last line to the following code:
for (s2 = s.replace(",3,", ",")); !s.equals(s2); ) {
  s = s2;
  s2 = s2.replace(",3,", ",");
}

btw: Why I didn't use a while loop? Because using I while loop I would have had to declare s2 outside the loop.

Ravi Venkataraman

Posts: 80
Nickname: raviv
Registered: Sep, 2004

Re: Isolate/remove parts of a String Posted: Apr 6, 2005 1:44 PM
Reply to this message Reply
Another option is to add the separator "," at the start and end of the string. Thus,
String s = "0,1,3,6";
 
s = ("," + s + ",").replace(",3,", ",").substring(1);

The last substring is to ensure that the leading "," that we introduced is removed.

You could make the code more robust by ensuring that the "," is added at the beginning or end only if it did not exist there.

Shaitan

Posts: 11
Nickname: shaitan00
Registered: Feb, 2005

Re: Isolate/remove parts of a String Posted: Apr 7, 2005 12:46 AM
Reply to this message Reply
Now that is a LOT of good answers, thank you all.
Few comments
- The values are limited to ONE digit (0,1,2,3,4,5,6,7,8,9)
- There can only be ONE instance of each value (for example there will never be more then one 3)
- I want to remove any one digit (not just a "3"), so I assume I just replace the "3" by a variable in the examples below.

Few questions:
s = ("," + s + ",").replace(",3,", ",").substring(1);
Wouldn't this leave a "," at the end? So I would have "0,1,6,"?

"You could bee naive enough to try replace("3", ""), but
that ould cause problems with numbers wich have 2 or
more digits."
Wouldn't this leave an extra "," So I would have "0,1,,6"?


So I guess the BEST solution for my needs would be:
String s = "0,1,3,6";
String[] nums = s.split(",");
String s2 = "";
for (int i = 0; i < nums.length; i++)
if (!"3".equals(nums)
s2 += "," + nums;
if (s2.length() > 0)
s2 = s2.substring(1);

Agree?
Thanks,

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Isolate/remove parts of a String Posted: Apr 7, 2005 1:21 AM
Reply to this message Reply
Q:
s = ("," + s + ",").replace(",3,", ",").substring(1);
Wouldn't this leave a "," at the end? So I would have "0,1,6,"?

A: You're right. Of course after that you have to remove the characters you added first.
The right syntax would be:
s = ("," + s + ",").replaceFirst(",3,", ",").substring(1).substring(0, Math.max(s.length()-2, 0));


Q:
"You could bee naive enough to try replace("3", ""), but
that ould cause problems with numbers wich have 2 or
more digits."
Wouldn't this leave an extra "," So I would have "0,1,,6"?

A: Right. My fault. Sorry



I'd say the best solution would be to use the syntax as show above.

Ths split method would works great and it would be the only useful solution if you had numbers of different lengtht, if you wanted to remove more numbers or whatever.
But it is slow.
It searches for all positions of the separator, creates a Vector of Strings, stores it in an array. After that you still have to check the single Strings and to create the new String.

For your problem we need only 2 concatenations, 1 replace operation (1 search, 2 concatenations) and 2 substring operations, not counting the operations needed to create a String and the numerical and logical operations.

Try to compare the speed of the 2 methods.
Let them run for a few thousand times (the same operatio over and over again) to see the difference.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Isolate/remove parts of a String Posted: Apr 8, 2005 10:13 AM
Reply to this message Reply
Hmm...

I'd just split() the string, work with the parts and then join() them back together and return the resultant string. Java has the split(), but you'll have to write the join() yourself (I think; I couldn't find something like it in the Java API docs). It would take an array of stings and a separator string and would join them all into one string with the separator between each of them (you might use a StringBuffer in your implementation of this).

It may be easier to manipulate the "parts" if they are put into an List instead of an array. You may also want to convert them to ints, or not as suits your purpose.

Flat View: This topic has 6 replies on 1 page
Topic: running applets Previous Topic   Next Topic Topic: layer implementation

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use