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.

Recommended Answers

All 3 Replies

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

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.

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);
    }
}

``

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.