Get string between two strings in a string

前端 未结 23 2198
别跟我提以往
别跟我提以往 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:25

    You can use the extension method below:

    public static string GetStringBetween(this string token, string first, string second)
        {            
            if (!token.Contains(first)) return "";
    
            var afterFirst = token.Split(new[] { first }, StringSplitOptions.None)[1];
    
            if (!afterFirst.Contains(second)) return "";
    
            var result = afterFirst.Split(new[] { second }, StringSplitOptions.None)[0];
    
            return result;
        }
    

    Usage is:

    var token = "super exemple of string key : text I want to keep - end of my string";
    var keyValue = token.GetStringBetween("key : ", " - ");
    
    0 讨论(0)
  • 2020-11-22 10:26

    Regex is overkill here.

    You could use string.Split with the overload that takes a string[] for the delimiters but that would also be overkill.

    Look at Substring and IndexOf - the former to get parts of a string given and index and a length and the second for finding indexed of inner strings/characters.

    0 讨论(0)
  • 2020-11-22 10:26
     string str="super exemple of string key : text I want to keep - end of my string";
            int startIndex = str.IndexOf("key") + "key".Length;
            int endIndex = str.IndexOf("-");
            string newString = str.Substring(startIndex, endIndex - startIndex);
    
    0 讨论(0)
  • 2020-11-22 10:26

    If you are looking for a 1 line solution, this is it:

    s.Substring(s.IndexOf("eT") + "eT".Length).Split("97".ToCharArray()).First()
    

    The whole 1 line solution, with System.Linq:

    using System;
    using System.Linq;
    
    class OneLiner
    {
        static void Main()
        {
            string s = "TextHereTisImortant973End"; //Between "eT" and "97"
            Console.WriteLine(s.Substring(s.IndexOf("eT") + "eT".Length)
                               .Split("97".ToCharArray()).First());
        }
    }
    
    0 讨论(0)
  • 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);                      
        }
    
    0 讨论(0)
  • 2020-11-22 10:27
    var matches = Regex.Matches(input, @"(?<=key :)(.+?)(?=-)");
    

    This returns only the value(s) between "key :" and the following occurance of "-"

    0 讨论(0)
提交回复
热议问题