How can I use regex or substring and index of to get a numbet from a string ?

Chocolade 516 Reputation points
2022-01-07T23:06:20.76+00:00

I don't know what is the number and the number length but I know he is between this two commas runs":[{"text":"34"

In this case the number is 34 but it can be any number and I want to extract the number any number between this two commas.

My problem now is how ot build this string totalVideosText":{"runs":[{"text":""

I tried this

string subjectstring = totalVideosText":{"runs":[{"text":""

but I'm getting errors on the right side of the string totalVideosText":{"runs":[{"text":""

I want to get any number there is between the last two commas ""
The two commas in the end "" are part of the string not commas of the string line.

It can be for example :

totalVideosText":{"runs":[{"text":"34" or totalVideosText":{"runs":[{"text":"2345" and I want to get the number any number between this commas.

Id regex is slow and expensive for this then maybe using indexof and substring ? anyway the problem now is to build the string.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,811 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,095 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,011 Reputation points
    2022-01-08T11:13:13.477+00:00

    It's unclear about the value as what you have will not work so I escaped quotes.

    private void ExampleButton_Click(object sender, EventArgs e)
    {
        string subjectstring = "totalVideosText\":{\"runs\":[{\"text\":\"34";
        Debug.WriteLine(ExtractNumber(subjectstring));
    }
    public string ExtractNumber(string original) 
        => new string(original.Where(char.IsDigit).ToArray());
    
    0 comments No comments

  2. WayneAKing 4,921 Reputation points
    2022-01-09T06:40:43.673+00:00

    First, a naming correction:

    As you may have picked up from Karen's reply, the character
    " is a quote not a comma. It is sometimes called a double-quote
    character.

    The string constructed by Karen does not have a quote character
    after the number. However, with or without a trailing quote
    character the method shown will extract the numeric substring.
    Note that it uses Linq to do the extraction.

    ff you wish to try your original idea of using Substring and
    IndexOf, this example which uses LastIndexOf should work also:

    string subj = "totalVideosText\":{\"runs\":[{\"text\":\"34\"";
    string num =
     subj.Substring(subj.TrimEnd('"').LastIndexOf('"')+1).TrimEnd('"');
    
    • Wayne
    0 comments No comments

  3. Jose Zero 571 Reputation points
    2022-01-09T13:34:55.563+00:00

    Seems you getting data from Json Array, perhaps make a class and deserialize json string give you more control.
    system-text-json-how-to

    In addition to codes already posted, if you want give a chance to Regex, you can get number with:

    string subj = "totalVideosText\":{\"runs\":[{\"text\":\"34\"";  
    string myNumber = System.Text.RegularExpressions.Regex.Replace(subj, @"\D", ""); // Replace Any non-digit  
    
    0 comments No comments