views:

84

answers:

4

I need a function or some code so I can find out what the number at the start is; Basically need to know the number at the start; The number can be 1 or 2 digits long

35|http:\/\/v10.lscache3.c.youtube.com\/videoplayback?ip=0.0.0.0&sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor%2Coc%3AU0dXSlZRTl9FSkNNN19OS1JJ&fexp=900808%2C907501&algorithm=throttle-factor&itag=35&ipbits=0&burst=40&sver=3&expire=1285660800&key=yt1&signature=D3917CA6F0C717D3C5DA6B35C7AB5DD6947A703C.424847792EDBA19DFD472068ED827DD18BA57455&factor=1.25&id=4e7eb2854bb9f182,34
+5  A: 

Use a regular expression:

using System.Text.RegularExpressions;

str = "35|http:\/\/v10.lscache3.c.youtube.com\/videoplayback...";

Regex r = new Regex(@"^[0-9]{1,2}");
Match m = r.Match(str);    
if(m.Success) {
    Console.WriteLine("Matched: " + m.Value);
} else {
    Console.WriteLine("No match");
}

will capture 1-2 digits at the beginning of the string.

burkestar
can use `\d` in place of `[0-9]` and the outter `()` s aren't necessary methinks
Mark
or [:digit:] should work too...parenthesis are needed for capture group
burkestar
...and you can use m.Success() if you expect there may be lines without numbers
Les
@burkestar in this case there's no need for the capture group. Mark is right that they can be omitted. Keeping them doesn't hurt, but by using `m.Value` you're actually not referencing the capture group. To do so you would use `m.Groups[1].Value`. Since this regex starts at the beginning of the string and isn't matching anything but digits you can omit the `()` and use `m.Value`.
Ahmad Mageed
@Les Good point, but it's a property, so just `m.Success`: `if (m.Success) { // do something } else { // no match }`
Ahmad Mageed
-1. abuse of regex when it's not really the best way to do it
Lie Ryan
regex is efficient, flexible if more advanced cases need to be considered, easy to understand and works since .NET 1.1. Linq requires .NET Framework 3.5.
burkestar
using regex just to capture 1 or 2 digits in a well-known position (index 0) is like using a sledge hammer to kill an ant
Lie Ryan
@Lie Ryan - 1) it is the OPs choice, 2) simple requirements seldom remain simple, 3) others reading this may have similar problem but need a more flexible solution than int.Parse().
Les
+5  A: 

You can use LINQ:

string s = "35|...";

int result = int.Parse(new string(s.TakeWhile(char.IsDigit).ToArray()));

or (if the number is always followed by a |) good ol' string manipulation:

string s = "35|...";

int result = int.Parse(s.Substring(0, s.IndexOf('|')));
dtb
+1 looks like you were first with the LINQ approach :)
Ahmad Mageed
Thank-you this is a simple self-explanatory answer. It is very useful to young programmers like me.
Liam E-p
can also use split and limit the number of splits, if he wants to do something with the other chunk
Mark
How would you remove the number after you have parsed it?
Liam E-p
Like the use of TakeWhile, I haven't used this extention as yet.
Harry
@Liam: Exactly. Try `s.Split(new char[] {'|'}, 2)` instead. it returns an array, the first one containing your number as a string, which you can `int.Parse`, and the second one containing the rest of the string w/ the number removed
Mark
A: 

if you know that the number is always going to be 2 digits:

string str = "35|http:\/\/v10.lscache3.c.youtube.com\/videoplayback?...";
int result;
if (!int.TryParse(str.Substring(0, 2), out result)) {
    int.TryParse(str.Substring(0, 1), out result)
}
// use the number

if you're not sure how long the number is, look at the .indexOf() approach by dtb. If you need something much more complex, only then consider using regex.

Lie Ryan
A: 

You can get the two first characters and convert to int.

var s = "a35|...";
short result = 0;
bool isNum = Int16.TryParse(s.Substring(0, 2), out result);
BrunoLM