如何正确使用 Matcher 获取字符串的前30个字符?

huangapple go评论138阅读模式
英文:

How do I properly use Matcher to retrieve first 30 chars of a String?

问题

我的目标是返回用户输入的字符串的前30个字符,并在电子邮件主题行中返回它。

我的当前解决方案是这样的:

Matcher matcher = Pattern.compile(".{1,30}").matcher(Item.getName());
String subject = this.subjectPrefix + "You have been assigned to Item Number " + Item.getId() + ": " + matcher + "...";

对于 matcher 返回的内容是 "java.util.regex.Matcher[pattern=.{1,30} region=0,28 lastmatch=]"。

英文:

My goal is to return the first 30 characters of a user entered String and its returned in an email subject line.

My current solution is this:

 Matcher matcher = Pattern.compile(".{1,30}").matcher(Item.getName());
    String subject = this.subjectPrefix + "You have been assigned to Item Number " + Item.getId() + ": " + matcher + "...";

What is being returned for matcher is "java.util.regex.Matcher[pattern=.{1,30} region=0,28 lastmatch=]"

答案1

得分: 3

我认为最好使用String.substring()

public static String getFirstChars(String str, int n) {
    if(str == null)
        return null;
    return str.substring(0, Math.min(n, str.length()));
}

如果你真的想使用regexp,那么这是一个示例:

public static String getFirstChars(String str, int n) {
    if (str == null)
        return null;

    Pattern pattern = Pattern.compile(String.format(".{1,%d}", n));
    Matcher matcher = pattern.matcher(str);
    return matcher.matches() ? matcher.group(0) : null;
}
英文:

I think it's better to use String.substring():

public static String getFirstChars(String str, int n) {
    if(str == null)
        return null;
    return str.substring(0, Math.min(n, str.length()));
}

In case you really want to use regexp, then this is an example:

public static String getFirstChars(String str, int n) {
    if (str == null)
        return null;

    Pattern pattern = Pattern.compile(String.format(".{1,%d}", n));
    Matcher matcher = pattern.matcher(str);
    return matcher.matches() ? matcher.group(0) : null;
}

答案2

得分: 2

好的,如果您真的需要使用Matcher,那么可以尝试:

Matcher matcher = Pattern.compile(".{1,30}").matcher("123456789012345678901234567890");
if (matcher.find()) {
    String subject = matcher.group(0);
}

但更好的方法是使用substring方法:

String subject = "123456789012345678901234567890".substring(0, 30);
英文:

Well, if you really need to use Matcher, then try:

Matcher matcher = Pattern.compile(".{1,30}").matcher("123456789012345678901234567890");
if (matcher.find()) {
    String subject = matcher.group(0);
}

But it would be better to use the substring method:

String subject = "123456789012345678901234567890".substring(0, 30);

答案3

得分: 2

我个人也会使用 String 类的 substring 方法。

然而,不要认为你的字符串至少有30个字符,我猜这可能是你的问题的一部分:

String itemName = "lorem ipsum";
String itemDisplayName = itemName.substring(0, itemName.length() < 30 ? itemName.length() : 30);
System.out.println(itemDisplayName);

这里使用了三元运算符,其中有一个布尔条件,然后是真和假的情况。所以如果你的字符串长度小于30个字符,我们将使用整个字符串,避免 java.lang.StringIndexOutOfBoundsException 异常。

英文:

I personally would use the substring method of the String class too.

However, don't take it for granted that your string is at least 30 chars long, I'd guess that this may have been part of your problem:

	String itemName = &quot;lorem ipsum&quot;;
	String itemDisplayName = itemName.substring(0, itemName.length() &lt; 30 ? itemName.length() : 30);
	System.out.println(itemDisplayName);

This makes use of the ternary operator, where you have a boolean condition, then and else. So if your string is shorter than 30 chars, we'll use the whole string and avoid a java.lang.StringIndexOutOfBoundsException.

答案4

得分: 1

使用 substring 替代。

String str = "....";
String sub = str.substring(0, 30);
英文:

use substring instead.

String str = &quot;....&quot;;
String sub = str.substring(0, 30);

huangapple
  • 本文由 发表于 2020年9月15日 22:42:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63904267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定