0

I have a list of String (List <String> titles) like this:

                    "xxxl54.AE",
                    "xxxl56.AP",
                    "xxxl57.AE",
                    "xxxl58.VP",
                    "xxxl59.WR",
                    "xxxl56.SD"

But I want to get this:

                    "xxxl54",
                    "xxxl56",
                    "xxxl57",
                    "xxxl58",
                    "xxxl59",
                    "xxxl56"

How do I delete the part that is to the right of the point?

1

2 Answers 2

3

If you want to replace the strings in your original list:

titles.replaceAll(e -> e.split("\\.")[0]);

Example:

List<String> titles = new ArrayList<>();
titles.add("xxxl54.AE");
titles.add("xxxl56.AP");
titles.add("xxxl57.AE");

System.out.println("Before: " + titles);

titles.replaceAll(e -> e.split("\\.")[0]);

System.out.println("After: " + titles);
Sign up to request clarification or add additional context in comments.

2 Comments

Excuse me can you write all the steps ??
That is just a oneliner. There are no more steps.
3

You need to use combination of default String methods String.substring and String.indexOf

To modify each item of the list, you could use stream().map and to save it again as the modified list collect(Collectors.toList())

List <String> titles = new ArrayList<>();
titles.add("xxxl54.AE");
titles = titles.stream().map(it -> it.substring(0, it.indexOf("."))).collect(Collectors.toList());

But be careful - if one of the items doesn't have a "." in its content, you need to handle it separately:

List <String> titles = new ArrayList<>();
titles.add("xxxl54.AE");
titles.add("xxxl00AE");
titles = titles.stream().map(it -> {
    int dotIndex = it.indexOf(".");
    if(dotIndex != -1) {
        it.substring(0, dotIndex);
    }
    return it;
}).collect(Collectors.toList());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.