-3

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=]"

  • 6
    Have you ever heard of the substring method? – maio290 Sep 15 '20 at 14:43
  • 3
    Have you thought about using Item.getName().substring(0,30) ? – Quazan Sep 15 '20 at 14:45
  • You can find many tutorials on how to use regular expression patterns in Java. For this task you do not even need the power of RegEx though. Here is a tutorial link: https://www.vogella.com/tutorials/JavaRegularExpressions/article.html – luksch Sep 15 '20 at 14:52
  • Does this answer your question? [Using Regular Expressions to Extract a Value in Java](https://stackoverflow.com/questions/237061/using-regular-expressions-to-extract-a-value-in-java) – Ivar Sep 15 '20 at 14:52
  • Does this answer your question? [How do I get the first n characters of a string without checking the size or going out of bounds?](https://stackoverflow.com/questions/1583940/how-do-i-get-the-first-n-characters-of-a-string-without-checking-the-size-or-goi) – Ivar Sep 15 '20 at 14:53

4 Answers4

3

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;
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
2

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);
AndreiXwe
  • 723
  • 4
  • 19
2

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 = "lorem ipsum";
    String itemDisplayName = itemName.substring(0, itemName.length() < 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.

maio290
  • 6,440
  • 1
  • 21
  • 38
1

use substring instead.

String str = "....";
String sub = str.substring(0, 30);
Hemant Patel
  • 3,160
  • 1
  • 20
  • 29