1

I want to reduce the length of my input string to max 20 characters, but I don't want't to break the string in the middle of a word.

// show me 20 char:   12345678901234567890 
string inputString = "This is an example user input which has to be shorten at a white space";
if (inputString.length > 20)
{
    shortenString = inputString.SubString(0, 21); // <-- "This is an example us"
    
    // I need a regex to match everything until the last white space

    // final output: "This is an example"
}
Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137
  • `var output = inputString.Split(' ').Aggregate(new List() { "" }, (a, x) => { if (a.Last().Length + x.Length + 1 < 20) { a[a.Count - 1] = $"{a.Last()} {x}".Trim(); } else { a.Add(x); } return a; });` – Enigmativity Jun 23 '20 at 01:20
  • Why specifically regex? – Enigmativity Jun 23 '20 at 01:20
  • 1
    ```(.{0,20})\s``` this regex will capture groups up to 20 characters, ending in whitespace – Ced Jun 23 '20 at 01:22
  • @Ced - how does that limit the line length to 20? – Enigmativity Jun 23 '20 at 01:22
  • @Enigmativity .{0,20} will match between 0 and 20 of the preceding character, then \s will match a whitespace. – Ced Jun 23 '20 at 01:25
  • `var result = shortenString.Contains(" ") ? shortenString.Substring(0, shortenString.LastIndexOf(" ")) : shortenString;` is one line. – Rufus L Jun 23 '20 at 01:29
  • @Ced - That will match up to 21 characters. – Enigmativity Jun 23 '20 at 01:32
  • @Enigmativity the capturing group, designated by the ( ), will limit to 20 characters. – Ced Jun 23 '20 at 01:34
  • @Ced - It would also need to cover strings that don't end in a whitespace and are shorter than 20 characters. – Enigmativity Jun 23 '20 at 01:34
  • 1
    @Enigmativity why don't you re-open the question, seeing as it clearly isn't a duplicate, then OP can clarify whether my answer is acceptable. Seeing as he's the one asking the question, I'd rather take his opinion on it than yours – Ced Jun 23 '20 at 01:38
  • @HoomanBahreini careful with that regex you linked, it will not limit to 20 characters, but capture everything up to the last whitespace – Ced Jun 23 '20 at 02:09
  • 1
    @HoomanBahreini - If you have an answer please post it. However, the link you posted doesn't answer the question you've asked. – Enigmativity Jun 23 '20 at 02:17

2 Answers2

2

(.{0,20})(\s|$) this regex will capture groups up to 20 characters, ending in whitespace (or end of string)

Ced
  • 1,117
  • 1
  • 8
  • 16
  • @Enigmativity correct. I've updated it to also match end of string, which will now capture "Hello world" – Ced Jun 23 '20 at 02:40
  • 1
    @Ced: Thanks for your help, I used your original regex: `^(.{0,20})\s` with a minor change (added `^` to ensure it counts from beginning of string)... I didn't use the end anchor `$`... instead added an if condition to ignore cases where input is too short. – Hooman Bahreini Jun 23 '20 at 02:56
1

I used the RegEx from @Ced answer, this is the final extension method:

public static string ShortenStringAtWhiteSpaceIfTooLong(this string str, int maxLen)
{
    if (str.Length <= maxLen)
    {
        return str;
    }

    string pattern = @"^(.{0," + maxLen.ToString() + @"})\s"; // <-- @"^(.{0,20})\s" 
    Regex regEx = new Regex(pattern);
    var matches = regEx.Matches(str);

    if (matches.Count > 0)
    {
        return matches[0].Value.Trim();
    }
    else
    {
        return str.Substring(0, maxLen).Trim();
    }
}
Hooman Bahreini
  • 14,480
  • 11
  • 70
  • 137