0

Alright so here is my problem. Basically I have a string with 4 words in it, with each word seperated by a #. What I need to do is use the substring method to extract each word and print it out. I am having trouble figuring out the parameters for it though. I can always get the first one right, but the following ones generally have problems. Here is the first piece of the code:

word = format.substring( 0 , format.indexOf('#') );

Now from what I understand this basically means start at the beginning of the string, and end right before the #. So using the same logic, I tried to extract the second word like so:

wordTwo = format.substring ( wordlength + 1 , format.indexOf('#') ); 
//The plus one so I don't start at the #.

But with this I continually get errors saying it doesn't exist. I figured that the compiler was trying to read the first # before the second word, so I rewrote it like so:

wordTwo = format.substring (wordlength + 1, 1 + wordLength + format.indexOf('#') );

And with this it just completely screws it up, either not printing the second word or not stopping in the right place. If I could get any help on the formatting of this, it would be greatly appreciated. Since this is for a class, I am limited to using very basic methods such as indexOf, length, substring etc. so if you could refrain from using anything to complex that would be amazing!

4

6 回答 6

4

If you have to use substring then you need to use the variant of indexOf that takes a start. This means you can start look for the second # by starting the search after the first one. I.e.

wordTwo = format.substring ( wordlength + 1 , format.indexOf('#', wordlength + 1 ) );

There are however much better ways of splitting a string on a delimiter like this. You can use a StringTokenizer. This is designed for splitting strings like this. Basically:

StringTokenizer tok = new StringTokenizer(format, "#");
String word = tok.nextToken();
String word2 = tok.nextToken();
String word3 = tok.nextToken();

Or you can use the String.split method which is designed for splitting strings. e.g.

String[] parts = String.split("#");
String word = parts[0];
String word2 = parts[1];
String word3 = parts[2];
于 2012-09-15T04:57:42.837 回答
2

You can go with split() for this kind of formatting strings.

For instance if you have string like,

String text = "Word1#Word2#Word3#Word4";

You can use delimiter as,

String delimiter = "#";

Then create an string array like,

  String[] temp;

For splitting string,

temp = text.split(delimiter);

You can get words like this,

temp[0] = "Word1";
temp[1] = "Word2";
temp[2] = "Word3";
temp[3] = "Word4";
于 2012-09-15T04:56:58.343 回答
1

Use split() method to do this with "#" as the delimiter

String  s = "hi#vivek#is#good";
String temp = new String();

String[] arr = s.split("#");

for(String x : arr){

  temp = temp + x;
}

Or if you want to exact each word... you have it already in arr

arr[0] ---> First Word
arr[1] ---> Second Word
arr[2] ---> Third Word
于 2012-09-15T04:57:19.490 回答
0

I suggest that you've a look at the Javadoc for String before you proceed further.

Since this is your homework, I'll give you a couple of hints and maybe you can solve it yourself:

  • The format for subString is public void subString(int beginIndex, int endIndex). As per the javadoc for this method:

Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

Note that if you've to use this method, understand that you'll have to shift your beginIndex and endIndex each time because in your situation, you'll have multiple words that are separated by #.

  • However if you look closely, there's another method in String class that might be helpful to you. That's the public String[] split(String regex) method. The javadoc for this one states:

Splits this string around matches of the given regular expression. This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

The split() method looks pretty interesting for your case. You can split your String with the delimiter that you have as the parameter to this method, get the String array and work with that.

Hope this helps you to understand your problem and get started towards a solution :)

于 2012-09-15T04:59:55.433 回答
0

Since this is a home work, it may be better to have try to write it your self. But I will give a clue.

Clue:

The indexOf method has another overload: int indexOf(int chr, int fromIndex) which find the first character chr in the string from the fromIndex.

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html

From this clue, the program will look something like this:

  1. Find the index of the first '#' from the start of the string.
  2. Extract the word from 0th character to that index.
  3. Find the index of the first '#' from the character AFTER the first '#'.
  4. Extract the word from the first '#' that index. ... Just do it until you get 4 words or the string ends.

Hope this helps.

于 2012-09-15T05:09:37.893 回答
0

I don't know why you're forced to use String#substring, but as others have mentioned, it seems like the wrong method for the kind of functionality you need.

String#split(String regex) is what you would use for such a problem, or, if your input sequence is something you don't control, I would suggest you look at the overloaded method String#split(String regex, int limit); this way you can impose a limit on the amount of matches you make, controlling your resulting array.

于 2012-09-15T05:15:49.160 回答