How to Obtain the Last Word of a String

Java: Simplest way to get last word in a string

String test =  "This is a sentence";
String lastWord = test.substring(test.lastIndexOf(" ")+1);

How to find the last word in a string

String#lastIndexOf and String#substring are your friends here.

chars in Java can be directly converted to ints, which we'll use to find the last space. Then we'll simply substring from there.

String phrase = "The last word of this sentence is stackoverflow";
System.out.println(phrase.substring(phrase.lastIndexOf(' ')));

This prints the space character itself too. To get rid of that, we just increment the index at which we substring by one.

String phrase = "The last word of this sentence is stackoverflow";
System.out.println(phrase.substring(1 + phrase.lastIndexOf(' ')));

If you don't want to use String#lastIndexOf, you can loop through the string and substring it at every space until you don't have any left.

String phrase = "The last word of this sentence is stackoverflow";
String subPhrase = phrase;
while(true) {
String temp = subPhrase.substring(1 + subPhrase.indexOf(" "));
if(temp.equals(subPhrase)) {
break;
} else {
subPhrase = temp;
}
}
System.out.println(subPhrase);

How to find the last word in a String in Java?

Here is my code:

myString = myString.trim();
String[] wordList = myString.split("\\s+");
System.out.println(wordList[wordList.length-1]);

obtain last word from a string in python

Using re.findall for a regex option, we can try:

inp = 'UPPER(\"Sales\".\"PRODUCTS\".\"PRODUCT_NAME\" )'
output = re.findall(r'^.*\b(\w+).*$', inp)
print(output[0]) # prints PRODUCT_NAME

For an explanation of how this works, the regex pattern will match everything up to the final word boundary, followed by the final word. We then access this match to print the result you want.

How to obtain the length of the last word in the string

Maybe try this,

public int lengthOfLastWord(String s) {

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

return arr[arr.length-1].length();

}

Get the last word of string

For this case, you can first trim the string:

String s = "hello is my new car ".trim();

Trim removes all trailing and leading spaces.

Then you can split the String like:

String[] words = s.split(" ");

Once you have that you can simply get the last index which will be the last word:

String lastWord = words[words.length - 1];

Ofcourse, for more complex issues regex would be a better option.

UPDATE:

In order to remove this word from the string you can simply replace it:

String withoutWord = s.replace(lastWord, "");

How to extract the first and final words from a string?

You have to firstly convert the string to list of words using str.split and then you may access it like:

>>> my_str = "Hello SO user, How are you"
>>> word_list = my_str.split() # list of words

# first word v v last word
>>> word_list[0], word_list[-1]
('Hello', 'you')

From Python 3.x, you may simply do:

>>> first, *middle, last = my_str.split()

How to get last word name from a string

Use String#split method to split based on delimiter / and get the last element of the array using Array#pop method.

var text = 'http://xxx:9696/images/FSDefault.jpg';
console.log(text.split('/').pop())

Remove first and last word from a string(php))

You can try Regular expressions

$string = "12121";
$trimmed = preg_replace('(^.)', '', $string);
$trimmed = preg_replace('(.$)', '', $trimmed);
print($trimmed);

but it seems overkill to use regex in this
so substr(like @aqib mentioned) might be the one you're looking for

$string = "12121";
$trimmed = substr($string, 1, -1);
print($trimmed);


Related Topics



Leave a reply



Submit