regex - C# Getting value between two same characters -
usually, can string value between 2 characters. question is, how value between 2 same characters.
for example:
string full_value = "http://stackoverflow.com/questions/9367119/title-goes-here";
in example, how can extract value 9367119
entire string?
the solution use doesn't work since 9367119
has same /
characters right , left of it.
here's have far:
this works values doesn't have 2 same characters left , right. such as: /dog\ can replace /
, \
solution
public static string between(string full_value, string a, string b) { int posa = full_value.indexof(a); int posb = full_value.lastindexof(b); if (posa == -1) { return ""; } if (posb == -1) { return ""; } int adjustedposa = posa + a.length; if (adjustedposa >= posb) { return ""; } return full_value.substring(adjustedposa, posb - adjustedposa); }
you split
, relevant part:
string s = "http://stackoverflow.com/questions/9367119/title-goes-here"; string[] sp = s.split('/'); console.writeline(sp[4]);
Comments
Post a Comment