Search code examples
c#razor-pages

ASP.NET Core, <br> text on specific length


<div class="input-container">
    <p>@review.review</p>
</div>

How can I break the text at a specific place?


Solution

  • there are multiple ways! the easiest way is by just using word-break in css. you can then adjust the amount of words by adjusting the width.

    .input-container > p {
        word-break: break-word;
        width: 125px
    }
    

    i assume you want to use c# so you could give regex a try

    using System;
    using System.Text.RegularExpressions;
    
    public class Example
    {
        public static void Main()
        {
            string pattern = @"(\b.{1,25})(?:\s+|$)";
            string substitution = @"";
            string input = @"Please write a program that breaks this text into small chucks. Each chunk should have a maximum length of 25 ";
            RegexOptions options = RegexOptions.Multiline;
            
            Regex regex = new Regex(pattern, options);
            string result = regex.Replace(input, substitution);
        }
    }