How do I get the nth word in a sentence or a set of strings with space delimiter?
Sorry for the change in the requirement.Thank you.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
By using
instr.instr(help,' ')returns the positional index of the first occurrence of the second argument in the first, inclusive of the string you’re searching for. i.e. the first occurrence of' 'in the string'hello my name is...'plus the space.substr(help, 1, instr(help,' ') - 1)then takes the input string from the first character to the index indicated ininstr(.... I then remove one so that the space isn’t included..For the nth occurrence just change this slightly:
instr(help,' ',1,n)is the nth occurrence of' 'from the first character. You then need to find the positional index of the next indexinstr(help,' ',1,n + 1), lastly work out the difference between them so you know how far to go in yoursubstr(.... As you’re looking for the nth, when n is 1 this breaks down and you have to deal with it, like so:This will also break down at n. As you can see this is getting ridiculous so you might want to consider using
regular expressions