Get string between two strings in a string

前端 未结 23 2506
别跟我提以往
别跟我提以往 2020-11-22 09:46

I have a string like:

\"super exemple of string key : text I want to keep - end of my string\"

I want to just keep the string which is betw

23条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 10:27

    I think this works:

       static void Main(string[] args)
        {
            String text = "One=1,Two=2,ThreeFour=34";
    
            Console.WriteLine(betweenStrings(text, "One=", ",")); // 1
            Console.WriteLine(betweenStrings(text, "Two=", ",")); // 2
            Console.WriteLine(betweenStrings(text, "ThreeFour=", "")); // 34
    
            Console.ReadKey();
    
        }
    
        public static String betweenStrings(String text, String start, String end)
        {
            int p1 = text.IndexOf(start) + start.Length;
            int p2 = text.IndexOf(end, p1);
    
            if (end == "") return (text.Substring(p1));
            else return text.Substring(p1, p2 - p1);                      
        }
    

提交回复
热议问题