简体   繁体   中英

Get a certain String from text

How do you get this string from this data? (The bolded part is what I want)

Id: 20 - Cow Amt: **10**

Id: 25 - Example Amt: **5**

I made this code to remove everything else except for the ID number

id = id.Replace("Id: ", "")

If (id.ToString.Length > 5) Then
    id = id.Substring(0, id.IndexOf("-") - 1).Trim()

But now I want to also get the Amt: number as well. The outcome of the code so far is this:

20

It just prints out the id. But how do you get the 10? So it would look like this:

20 10

Try the following

 var subjectString = "Id: 20 - Cow Amt: 10"; string[] values = Regex.Split(subjectString, @"\\D+"); foreach (string value in values) { int number; if (int.TryParse(value, out number)) { Console.WriteLine(value); } } 

Remeber to include using System.Text.RegularExpressions; in the references at the top

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM