Home > Enterprise >  Remove a part of string based on its given length with replaceAll in Java
Remove a part of string based on its given length with replaceAll in Java

Time:01-13

I know that there are different ways to solve this task, but I need a particular way using replaceAll() method. I just stuck with right condition in the expression.

So I have a method like this:

    public static void handleComments(List<Comment> comments, int maxTextLength) {
            comments.replaceAll(comment -> comment.getText().length() > maxTextLength ?  *what should be here?* : comment);
        }

    class Comment {
    private final String text;

    public Comment(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}

The case is next: I pass to the method some comments and max length of comment. The method should take list of comments and next, if comment length > maxTextLength, it returns new comment that is a copy of original comment, but shorter (with maxTextLength amount of characters), and if comment length < maxTextLength, it just returns the same comment (or it can be also a copy with the same amount of characters).

Update: Example is below - we have (enter it) limit of 30 characters per string and method cuts all characters in each comment if there are more (>) than 30 characters.

Sample Input:

30

What a nice view! Where is it ?

I do not know, I just found it on the internet!

Perfect!

Sample Output:

What a nice view! Where is it

I do not know, I just found it

Perfect!

CodePudding user response:

You never showed the comment class. Can we set the text on a comment? Can we create a new one giving the text on the constructor? I am assuming the latter.

Then replace *what should be here?* with

 new Comment(comment.getText().substring(0, maxTextLength-3)   "...")

CodePudding user response:

I guess that you need something like this:

public static void handleComments (List<Comment> comments, int maxTextLength) {

    comments.stream()
        .filter(comment -> comment.getText().length() > maxTextLength)
        .forEach(comment -> comment.setText(comment.getText().substring(0, maxTextLength - 1))
        );
  }

CodePudding user response:

There are so many ways to do this. I would opt for the most straight forward. This method only replaces comments that are too long.

class Comment {
    private String text;

    public Comment(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setText(String comment) {
        this.text = comment;
    }

    public String toString() {
        return text;
    }
}

List<Comment> comments = List.of(
        new Comment("What a nice view! Where is it ?"),
        new Comment("I do not know, I just found it on the internet!"),
        new Comment("Perfect!"));

comments.forEach(System.out::println);
System.out.println();
handleComments(comments, 30);
comments.forEach(System.out::println);

prints

What a nice view! Where is it ?
I do not know, I just found it on the internet!
Perfect!

What a nice view! Where is it 
I do not know, I just found it
Perfect!

The handle method. Just use a loop. Don't replace the comments if they don't need it or create a new object. Just update the required text using your setter.

public static void handleComments(List<Comment> comments,
        int maxTextLength) {
    comments.forEach(com -> {
        String txt = com.getText();
        int len = txt.length();

        if (len > maxTextLength) {
            com.setText(txt.substring(0, maxTextLength));
        }
    });
}


  • Related