0

Hello Guys,
If I have a string "Registration 00DL5876 Make" is there a way to match the first word and the last ("Registration" & "Make") and than pass the substring "00DL5876" to seperate string ? how about "Primera SR 2.0 D Colour" if i want "SR 2.0 D" ? I dont even know how where to start.

4
Contributors
3
Replies
19
Views
4 Years
Discussion Span
Last Post by Seldar
1

beginsWith and endsWith.
or, you can use the split method, with space as separator and keep all the elements of the returning array, but discard the first and the last one

0

You can use String methods: indexOf(' ') to find the position of the first space (end of first word), and lastIndexOf(' ') to find the position of the last space (start of last word), then use use substring to get the text between those two positions.

0

Hello,
If there is a whitespace inbetween the words forming your string, you can simply use the split() method.
I took the time and liberty to write a very small example program.

public class StrSplit {
    public static void main (String[] args){
        final String myString = "Registration 00DL5876 Make";
        String [] split = myString.split(" ");
        final String firstPart = split[0];
        final String middlePart= split[1];
        final String lastPart = split[2];

        System.out.println("The original string was: " + myString);
        System.out.println("The first word is: " + firstPart);
        System.out.println("The middle bit is: " + middlePart);
        System.out.println("The last word is: " + lastPart);
    }
}

``

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.