Extracting a value from a string

답변됨 Extracting a value from a string

  • 2012년 5월 1일 화요일 오후 7:13
     
     

    Hello,

    Can some one please tell me an easy way by which I can extract a value from this staring..What I am looking for is the word sale which I have made bold  but it can be any word in that place..but the format of this whole string will always be the same.

    Lists%2fLights%2fsale&FolderCTID=&View=%7bA7FB17B0%2dDCB1%2d4453%2dA6E9%2d6A729929DA73%7d.

    Thanks

모든 응답

  • 2012년 5월 1일 화요일 오후 7:14
     
     

    Is it always in the same index position?  I guess my question is really what do you mean by "the format of this whole string will always be the same"?

    That is, is it just that %2f is the delimiter and it's always the 3rd element?  Or that it always starts with exactly Lists%2fLights%2f


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...


  • 2012년 5월 1일 화요일 오후 7:18
     
     

    It always starts with exactly Lists%2fLights%2f..what I meant was that say this is our string

    Lists%2fLights%2fsale&FolderCTID=&View=%7bA7FB17B0%2dDCB1%2d4453%2dA6E9%2d6A729929DA73%7d.

    in this the sale word can be replace by anything like gadget,csharp or anything..I need to get that word ..Let me know if I need to clarify more.

    Thanks for the quick reply.

    • 편집됨 Sharepoint997 2012년 5월 1일 화요일 오후 7:25
    •  
  • 2012년 5월 1일 화요일 오후 7:23
     
     제안된 답변 코드 있음

    Well, a really easy way, providing the format stays exactly the same, is to just substring it:

    // just making consts for ease of use

    const string header = "Lists%2fLights%2f";             const string after = "&FolderCTID";

    // find start of after part, beginning after header then extract             var endPos = input.IndexOf(after, header.Length);             var word = input.Substring(header.Length, endPos - header.Length);

                  

    Though, of course, you could also do a RegEx as well...


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...


  • 2012년 5월 1일 화요일 오후 7:51
     
     

    That's working great ..Thank you ..but just in case if the other words are changing is it easy to do with regex like in the string

    Lists%2fLights%2fsale&FolderCTID=&View=%7bA7FB17B0%2dDCB1%2d4453%2dA6E9%2d6A729929DA73%7d.  if the second word lights is different then how can we use Regex to extract the word sale.

    Thanks

  • 2012년 5월 1일 화요일 오후 7:54
     
     

    That would depend on what parts of the input string are truly fixed.  That is, do you see this string as always being in the form:

    {something}%2f{something}%2f{target}&FolderCTID=&View={something}


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

  • 2012년 5월 1일 화요일 오후 8:01
     
     
    That is correct.
  • 2012년 5월 1일 화요일 오후 8:14
     
     답변됨 코드 있음

    You could create your regex then grab third third segment into a group, like this:

                Regex r = new Regex(@"\w*%2f\w*%2f(\w*)&FolderCTID=.*");
     
                var result = r.Match(input);
     
                if (result.Success)
                {
                    Console.WriteLine(result.Groups[1].Value);
                }

    The parenthesis around the third (\w*) means you want to capture that part of the match into a group, which will be stored in Group[1] if the match is good (Group[0]) contains the whole match.


    James Michael Hare

    Blog: http://www.geekswithblogs.net/BlackRabbitCoder

    Twitter: @BlkRabbitCoder

    There are 10 kinds of people in the world: those who know binary and those who don't...

    • 답변으로 표시됨 Sharepoint997 2012년 5월 1일 화요일 오후 8:18
    •  
  • 2012년 5월 1일 화요일 오후 8:17
     
     
    Beautiful..Thank you so much :)