Get the sub string between two input string - CSharp System

CSharp examples for System:String SubString

Description

Get the sub string between two input string

Demo Code


using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Text;
using System;//from  w ww . j  a  v a2s. c om

public class Main{
        /// <summary>
        /// Get the sub string between 'ket' and 'bra'.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="bra"></param>
        /// <param name="ket"></param>
        /// <returns></returns>
        public static string GetLastSubStringBetween(string text, char bra, char ket)
        {
            if (text == null) return null;
            int braIndex = text.LastIndexOf(bra);
            if (braIndex > -1)
            {
                int ketIndex = text.LastIndexOf(ket);
                if (ketIndex > braIndex)
                {
                    return text.Substring(braIndex + 1, ketIndex - braIndex - 1);
                }
            }
            return String.Empty;
        }
}

Related Tutorials