2017-07-16 25 views
-2

有這樣一條線,需要與С#:需要與С#。我怎麼能得到33?

"1/33 " 
"1/32 " 
"1/31 " 

我怎樣才能得到33?

在С++:

sprintf("*/%d&*", value) 

如何使用с#做到這一點?我需要一個int a = string(...),它會返回33?

+4

嚴重的是,什麼? – Guy

+0

你想打印:'「1/{something}」'?.. –

+0

我想問題是關於'正則表達式',他想從第一個字符串中獲得數字33。 – Ahmar

回答

2

選項1:使用Replace

string yourString="1/33 "; // Your string if I understand the Q 
yourString=yourString.Replace("1/","").Replace(" ", ""); // Replace 1/ and   with nothing Thank you @Mike 
int number; // The number that will be convert 
    if(int.TryParse(yourString, out number)) // Convert string to int if the string is a number 
    { 
     // Do something 
     // Number is 33 it this case 
    } 

選項2:使用Substring(@Mike評論)

string yourString="1/33 "; // Your string if I understand the Q 
string n33 = yourString.Substring(yourString.IndexOf("/") + 1, yourString.IndexOf("&") - 2); 
int number; // The number that will be convert 
     if(int.TryParse(n33 , out number)) // Convert string to int if the string is a number 
     { 
      // Do something 
      // Number is 33 it this case 
     } 
+1

我以爲有更完美的с#:string n33 = number.Substring(number.IndexOf(「/」)+ 1,number.IndexOf(「&」) - 2); – Mike

+1

string n33 = number.Replace(「1 /」,「」)。Replace(「 」,「」); – Mike