How to determine which number (with an arbitrary number of digits) is at the beginning of a line?
Some possible lines:
1123|http://example.com 2|daas
Which should return 1123 and 2.
You can use LINQ:
string s = "35|..."; int result = int.Parse(new string(s.TakeWhile(char.IsDigit).ToArray()));
or (if the number always follows |) good string manipulation:
|
string s = "35|..."; int result = int.Parse(s.Substring(0, s.IndexOf('|')));
Use regex:
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 write 1-2 digits at the beginning of the line.
, 2 :
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
, , .indexOf() dtb. - , .
int.
var s = "a35|..."; short result = 0; bool isNum = Int16.TryParse(s.Substring(0, 2), out result);
Source: https://habr.com/ru/post/1766885/More articles:The best way to avoid memory leaks for multiple buttons in a UITableViewCell is memoryMaintain top-k set in Java - javaIs there a way to index a varchar column as datetime? - sql-serverHaskell if statement for checking errors - haskellКогда InnoDB тайм-аут вместо того, чтобы сообщать о взаимоблокировке? - mysqlI need a container that supports efficient random access and installing and uninstalling O (k) - c ++https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1766887/what-kind-of-messaging-architectures-are-used-in-huge-scalable-sites-today&usg=ALkJrhh3AYhFZk5ouecNA_4egZSZyvEk4APython 3D graphics - pythonRemove parts of a dynamic array and produce others - c ++PHPUnit test How many times a function is called - phpAll Articles