user3379655
user3379655

Reputation: 258

How to get string after specific string?

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..

Upvotes: 3

Views: 21137

Answers (4)

Jodrell
Jodrell

Reputation: 35746

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.

Upvotes: 3

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101732

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();

Upvotes: 9

carlpett
carlpett

Reputation: 12613

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

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39443

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

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

Upvotes: 3

Related Questions