5 Replies - 567 Views - Last Post: 04 February 2012 - 05:10 PM Rate Topic: -----

Topic Sponsor:

#1 shango11  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 64
  • Joined: 06-July 11

How do you get the second word of a string?

Posted 04 February 2012 - 04:34 PM

The problem reads as follow:
Assume that sentence is a variable of type String that has been assigned a value. Assume furthermore that this value is a String consisting of words separated by single space characters with a period at the end. For example: "This is a possible value of sentence."

Assume that there is another variable declared, secondWord , also of type String. Write the statements needed so that the second word of the value of sentence is assigned to secondWord . So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord.

I should also note that I'm supposed to use indexOf and substring, and what I'm having trouble is finding the ending value for the substring.
Here is what I have:
int position = sentence.indexOf(" ");
secondWord = sentence.substring(position+1, );


The logic behind the code is that since I need the second word I look for the first space character in the string then for the substring I add 1 to "position" so I skip the space and start at the word, the problem is that I don't know what value I need to put for the ending because it can't be a set value since each string might be different.

Is This A Good Question/Topic? 0
  • +

Replies To: How do you get the second word of a string?

#2 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1785
  • View blog
  • Posts: 3,359
  • Joined: 19-March 11

Re: How do you get the second word of a string?

Posted 04 February 2012 - 04:42 PM

Try this, for starters:

1 get substring from the first space to the end of the string
2 from that substring, get the substring from the beginning to the first space, or to the end if there's no first space

There are some refinements you could make to it, but this should work, and I suspect you can write it, so it's a good place to start.
Was This Post Helpful? 1
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,072
  • Joined: 05-April 11

Re: How do you get the second word of a string?

Posted 04 February 2012 - 04:52 PM

You might as well just find the index of the first " ", and then find the next space. If there is no last space (the index will be -1), then you should just substring from the first space till the end of the line.

int start = sentence.indexOf(" ") + 1;
int end = sentence.indexOf(" ", start+1); //Search from start+1


Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1785
  • View blog
  • Posts: 3,359
  • Joined: 19-March 11

Re: How do you get the second word of a string?

Posted 04 February 2012 - 04:55 PM

Yes, that'd be one of the refinements I mentioned. :)
Was This Post Helpful? 0
  • +
  • -

#5 shango11  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 64
  • Joined: 06-July 11

Re: How do you get the second word of a string?

Posted 04 February 2012 - 05:08 PM

Thanks for the help guys! I didn't know I could manipulate indexOf in that way.
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1785
  • View blog
  • Posts: 3,359
  • Joined: 19-March 11

Re: How do you get the second word of a string?

Posted 04 February 2012 - 05:10 PM

It's all in the API

If you want to write java, you could do worse than reading down the whole list of methods of String and writing a program to exercise each one.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1