简体   繁体   English

在 Java 中使用 replaceAll 根据其给定长度删除字符串的一部分

[英]Remove a part of string based on its given length with replaceAll in Java

I know that there are different ways to solve this task, but I need a particular way using replaceAll() method.我知道有不同的方法可以解决这个任务,但我需要一种使用 replaceAll() 方法的特定方法。 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).该方法应该获取评论列表,接下来,如果评论长度 > maxTextLength,它返回新评论,它是原始评论的副本,但更短(具有 maxTextLength 个字符),如果评论长度 < maxTextLength,它只返回相同的内容评论(或者它也可以是具有相同字符数的副本)。

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.更新:示例如下 - 我们有(输入)每个字符串 30 个字符的限制,如果 (>) 超过 30 个字符,该方法将删除每个注释中的所有字符。

Sample Input:示例输入:

30 30

What a nice view!好美! Where is it ?它在哪里

I do not know, I just found it on the inte.net!我不知道,我只是在 inte.net 上找到它!

Perfect!完美的!

Sample Output:样本 Output:

What a nice view!好美! Where is it它在哪里

I do not know, I just found it我不知道,我刚找到

Perfect!完美的!

You never showed the comment class. Can we set the text on a comment?您从未显示评论 class。我们可以在评论上设置文本吗? Can we create a new one giving the text on the constructor?我们可以创建一个新的给构造函数上的文本吗? I am assuming the latter.我假设是后者。

Then replace *what should be here?* with然后将*what should be here?*替换为

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

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

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 .如果不需要,请不要替换评论或创建新的 object。只需使用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));
        }
    });
}


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM