John Ding
John Ding

Reputation: 31

How to read specific values in string?

For example, TOMILA RELEASE V6.24 , i want to get 6.24 i used

 if (txt.Contains("<TOMILA RELEASE")) 
 {
     int iStartIndex = txt.LastIndexOf("<TOMILA RELEASE") + 17;
     for (int i = 0; i < 50; i++) {
         if (txt[iStartIndex + i] == '>') break;
         currentRelease += txt[iStartIndex + i];

     }
 }

So, my question is if i want to get the specific 6 from TOMILA RELEASE V6.24, how could i get it?

Upvotes: 0

Views: 134

Answers (3)

Adil
Adil

Reputation: 148110

If you want to take first number in the string you can use following regular expression.

string s = "TOMILA RELEASE V6.24";
string digit = Regex.Match(s, "\\d").Value;

Here \d is for matching the digit, you can find more about regular expression in this tutorial, The 30 Minute Regex Tutorial

If you want to extract all number before dot then you can add + with \d and use do to end the extraction.

string number = Regex.Match(s, "\\d+.").Value.Replace(".","");

Upvotes: 1

Carbine
Carbine

Reputation: 7903

You can try LastIndexOf followed by Substring

var result = str.Substring(str.LastIndexOf('TOMILA RELEASE V') + 1);

Upvotes: 2

Joseph
Joseph

Reputation: 1064

If you want to get a specific portion of a string, you could use the below code

  string str = "6.24";
  var val = str.Substring(0, 1);

Upvotes: 0

Related Questions