Shaitan
Posts: 11
Nickname: shaitan00
Registered: Feb, 2005
|
|
Re: Isolate/remove parts of a String
|
Posted: Apr 7, 2005 12:46 AM
|
|
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,
|
|