【发布时间】:2021-01-02 08:29:30
【问题描述】:
我的目标是返回用户输入字符串的前 30 个字符,并将其返回到电子邮件主题行中。
我目前的解决方案是这样的:
Matcher matcher = Pattern.compile(".{1,30}").matcher(Item.getName());
String subject = this.subjectPrefix + "You have been assigned to Item Number " + Item.getId() + ": " + matcher + "...";
匹配器返回的是“java.util.regex.Matcher[pattern=.{1,30} region=0,28 lastmatch=]”
【问题讨论】:
-
你听说过substring方法吗?
-
你有没有想过使用 Item.getName().substring(0,30) ?
-
您可以找到许多关于如何在 Java 中使用正则表达式模式的教程。不过,对于这项任务,您甚至不需要 RegEx 的强大功能。这是一个教程链接:vogella.com/tutorials/JavaRegularExpressions/article.html