Splits a string that has an integer number as suffix (e.g. "text123") into its components. - CSharp System

CSharp examples for System:String Split

Description

Splits a string that has an integer number as suffix (e.g. "text123") into its components.

Demo Code

// This file is subject to the terms and conditions defined in
using System.Globalization;
using System;/*  w  ww  .  jav  a 2s  .c om*/

public class Main{
        /// <summary>
    /// Splits a string that has an integer number as suffix (e.g. "text123") into its components.
    /// </summary>
    /// <param name="str">
    /// A string that has an positive integer number as suffix. The suffix is optional. Examples:
    /// "text", "text123".
    /// </param>
    /// <param name="text">
    /// The text before the number suffix. "" if <paramref name="str"/> is <see langword="null"/> or
    /// empty.
    /// </param>
    /// <param name="number">
    /// The positive integer number. -1 if <paramref name="str"/> does not contain a suffix.
    /// </param>
    /// <remarks>
    /// <para>
    /// This method should only be used if <paramref name="str"/> contains only letters and digits.
    /// If <paramref name="str"/> contains characters other than letters and digits the result is
    /// undefined.
    /// </para>
    /// <para>
    /// Here is a list of examples:
    /// </para>
    /// <list type="table">
    /// <listheader>
    /// <term>Input</term>
    /// <description>Output</description>
    /// </listheader>
    /// <item>
    /// <term><see langword="null"/></term>
    /// <description>"", -1</description>
    /// </item>
    /// <item>
    /// <term>""</term>
    /// <description>"", -1</description>
    /// </item>
    /// <item>
    /// <term>"text"</term>
    /// <description>"text", -1</description>
    /// </item>
    /// <item>
    /// <term>"123"</term>
    /// <description>"", 123</description>
    /// </item>
    /// <item>
    /// <term>"text123"</term>
    /// <description>"text", 123</description>
    /// </item>
    /// <item>
    /// <term>"123text"</term>
    /// <description>"123text", -1</description>
    /// </item>
    /// <item>
    /// <term>"123text456"</term>
    /// <description>"123text", 456</description>
    /// </item>
    /// </list>
    /// </remarks>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters")]
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly")]
    public static void SplitTextAndNumber(this string str, out string text, out int number)
    {
      if (String.IsNullOrEmpty(str))
      {
        text = String.Empty;
        number = -1;
      }
      else if (!Char.IsDigit(str, str.Length - 1))
      {
        text = str;
        number = -1;
      }
      else
      {
        // Separate text and number:
        int i = str.Length - 1;
        while (i > 0 && !Char.IsLetter(str[i - 1]))
          i--;

        number = Int32.Parse(str.Substring(i), CultureInfo.InvariantCulture);
        text = str.Substring(0, i);
      }
    }
}

Related Tutorials