Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Playing with Stings in Java...?
I have this activity question that is asking me to change any word that is given in brackets ( "<" & ">" ) into the word itself, without the brackets. For example, if y is "<div>" then x should evaluate to "div".
Below, I will test the value "<div>":
String y = "<div>";
String x;
Now, I must have a code...the only code I can think of would only eliminae the first bracket. I need to get rid of BOTH of them
x = y.substring(1); ---> only rids me of the 1st bracket
So now I ask...how could you get rid of the last backet. Assuming that this code can be applied to all strings of different lengths, I need a code that would apply to any string.
I tried:
x = y.substring(1, (y.length-1));
but that generates an error.
1 Answer
- SilentLv 71 decade agoFavorite Answer
Well, this generates an error because String.length() is a method, not a property, so you need parentheses after it. Also, if you read the documentation for the String.substring() method (something you should do before using any method anywhere) you'll see that you should be using y.length(), not y.length() - 1, because the substring does not include the character at the second index given.
All that said, if you are sure that the word itself does not contain any angle brackets that need to be retained, then the right way to do this is not to use substrings at all. You should instead use the String.replace() or String.replaceAll() methods to replace the angle brackets with empty Strings.
See the documentation for these methods at the links below.
Source(s): http://download.oracle.com/javase/6/docs/api/java/... http://download.oracle.com/javase/6/docs/api/java/... http://download.oracle.com/javase/6/docs/api/java/...