3

How to get string after text "playlist:" ?

var YT= "tag:youtube.com,2008:user:hollywoodlife09:playlist:PLDovhwKa3P88MwGzYxMDMfiAiiEWxAJYj" ;

What I did :

string[] s = YT.Split(':');

But it will give me array i.e s[0],s[1] ... and I am searching for something which can give result after specific text.

I want string after "playlist:", I know it may be easy with Regex,but currently I don't have any idea for Regex..

4 Answers 4

9

You can use Substring method

var output = inputString.SubString(inputString.LastIndexOf("playlist") + 8);

Or in this case it can be done using Last method via Split:

string output = YT.Split(':').Last();
Sign up to request clarification or add additional context in comments.

2 Comments

I'd use this method because this is quite a simple program and regex generally reduces readability of code greatly.
You can also split on "playlist:".
3

Using regex replace, remove everything before the :playlist: with empty string.

string playlist = Regex.Replace(YT, ".*:playlist:", "");

Comments

3

more reusably,

static IEnumerable<KeyValuePair<string, string>> SplitPairs(
    this string source,
    params char[] seperators)
{
    var values = source.Split(seperators);
    for(var i = 0; i < values.Length; i += 2)
    {
        yield return new KeyValuePair<string, string>(
            values[i],
            values[i + 1]);
    }
}

so you could do,

var yTlookup = YT.SplitPairs(':').ToDictionary(p => p.Key, p => p.Value);
var playList = yTLookup["playlist"];

or if you don't want an extension,

var segments = YS.Split(new[] { ':' });
var ySlookup = Enumerable.Range(0, segemnts.Length / 2)
    .ToDictionary(i => segments[i * 2], i => segments[(i * 2) + 1]);

so you can do,

var playlist = ysLookup["playlist"];

either approach pays off as soon as you want another value from the sequence.

Comments

1

The regex is .+playlist:([^:])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.