Assets/TextExtensions.cs
/// <summary>
/// Turns source string into an array of string segments - each with a set maximum width - for parsing or displaying.
/// </summary>
/// <param name="value">Input string to break up into segments.</param>
/// <param name="segmentSize">Maximum size of returned segment.</param>
/// <returns>Array of string segments as parsed from source string.</returns>
/// <remarks>Returns a single element array with an empty string if source string is null or empty.</remarks>
Turns source string into an array of string segments - each with a set maximum width - for parsing or displaying.
Turns source string into an array of string segments - each with a set maximum width - for parsing or displaying .
public static string[] GetSegments(this string value, int segmentSize)
        {
            if (segmentSize <= 0)
                throw new ArgumentOutOfRangeException(nameof(segmentSize), "segmentSize must be greater than zero.");
            if (string.IsNullOrWhiteSpace(value))
                return new[] { "" };
            List<string> segments = new List<string>();
            if (value.Contains(" "))
            {
                string[] lines = value.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string line in lines)
                {
                    string[] words = line.Split(new[] { " ", Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                    string segment = "";
                    foreach (string word in words)
                    {
                        if (segment.Length + word.Length > segmentSize)
                        {
                            segments.Add(segment.Trim());
                            segment = "";
                        }
                        segment += $"{word} ";
                    }
                    if (segment.Length > 0)
                        segments.Add(segment.Trim());
                }
            }
            else
            {
                int start = 0;
                while (start < value.Length)
                {
                    segments.Add(value.Substring(start, Math.Min(segmentSize, value.Length - start)));
                    start += segmentSize;
                }
            }
            return segments.ToArray();
        }
public static string [ ] GetSegments ( this string value , int segmentSize ) { if ( segmentSize <= 0 ) throw new ArgumentOutOfRangeException ( nameof ( segmentSize ) , " " ) ; if ( string . IsNullOrWhiteSpace ( value ) ) return new [ ] { " " } ; List < string > segments = new List < string > ( ) ; if ( value . Contains ( " " ) ) { string [ ] lines = value . Split ( new [ ] { Environment . NewLine } , StringSplitOptions . RemoveEmptyEntries ) ; foreach ( string line in lines ) { string [ ] words = line . Split ( new [ ] { " " , Environment . NewLine } , StringSplitOptions . RemoveEmptyEntries ) ; string segment = " " ; foreach ( string word in words ) { if ( segment . Length + word . Length > segmentSize ) { segments . Add ( segment . Trim ( ) ) ; segment = " " ; } segment += $" { word } " ; } if ( segment . Length > 0 ) segments . Add ( segment . Trim ( ) ) ; } } else { int start = 0 ; while ( start < value . Length ) { segments . Add ( value . Substring ( start , Math . Min ( segmentSize , value . Length - start ) ) ) ; start += segmentSize ; } } return segments . ToArray ( ) ; }
Turns source string into an array of string segments - each with a set maximum width - for parsing or displaying.
Turns source string into an array of string segments - each with a set maximum width - for parsing or displaying .
Array of string segments as parsed from source string.
Array of string segments as parsed from source string .
string
this
segmentSize
int
Maximum size of returned segment.
Maximum size of returned segment .
value
Input string to break up into segments.
Input string to break up into segments .
remarks
Returns a single element array with an empty string if source string is null or empty.
Returns a single element array with an empty string if source string is null or empty .