我怎样才能从字符串中获取数字? [英] How Can I Get Numbers From String?

查看:81
本文介绍了我怎样才能从字符串中获取数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的字符串:



s = 2 + 4

(s [0] = 2,s [1] = +,s [2] = 4)。

如何从中提取2,4?



i have a string like this :

s=2+4
(s[0]=2,s[1]=+,s[2]=4).
how can i extract 2, 4 from it ?

using System;

class program
{
    public static void Main()
    {
        int n,x=0,y=0,a,a1,b1,b,f=0,z,sum=0,q=0,l=0;
        char c;
        string s,s1,s2,s3;
        int [] sum1=new int [4];
        Console.WriteLine("Let's go :");
        s = Console.ReadLine();
        for (int i=0;i<s.length;i++)>
        {
            switch( s[i])
            {
                case '0':
                    n = Convert.ToInt32(s[q]);
                    string[,] zero = new string[7, 3] { { "0", "0", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", "0", "0" }, { "0", "0", "0" } };
                    for (int h=0;h<7;h++)
                    {
                        for (int j=0;j<3;j++)
                        {
                            Console.Write(zero[h,j]);
                        }
                        Console.WriteLine();
                     
                    }
                   sum1[q]=n;
                   q = q + 1;


                    
                    break;
                case '1':
                    n = Convert.ToInt32(s[i]);
                   string [,] one=new string[7,3] {{"1","1","1"},{" " ," ","1"},{" "," ","1"},{" "," ","1"},{" "," ","1"},{" "," ","1"},{" "," ","1"}};
                   for (int h = 0; h < 7; h++)
                   {
                       for (int j = 0; j < 3; j++)
                       {
                           Console.Write(one[h, j]);
                       }
                       Console.WriteLine();

                   }
                  
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '2':
                    n = Convert.ToInt32(s[i]);
                   string[,] two= new string[7, 3] { { "2","2","2" }, { " "," ","2" }, { " "," ","2" }, { "2","2","2" }, { "2"," "," " }, { "2"," "," " }, {"2","2","2" } };
                   for (int h = 0; h < 7; h++)
                   {
                       for (int j = 0; j < 3; j++)
                       {
                           Console.Write(two[h, j]);
                       }
                       Console.WriteLine();
                   }
                   x = 2;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '3':
                    n = Convert.ToInt32(s[i]);
                    string[,] three = new string[7, 3] { {"3","3","3" }, { " ", " ", "3" }, { " "," ","3" }, { "3","3","3" }, { " "," ","3" }, { " "," ","3" }, {"3","3","3" } };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(three[h, j]);
                        }
                        Console.WriteLine();
                    }
                    y = 3;
                     sum1[q]=n;
                   q = q + 1;

                    break;
                case '4':
                    n = Convert.ToInt32(s[i]);
                    string[,] four = new string[7, 3] { { "4"," ","4" }, {"4"," ","4"}, {"4"," ","4" }, { "4","4","4"}, { " "," ","4"}, { " ", " ","4"}, { " "," ","4"} };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(four[h, j]);
                        }
                        Console.WriteLine();
                    }
                    x = 4;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '5':
                    n = Convert.ToInt32(s[i]);
                    string[,] five = new string[7, 3] { { "5","5","5" }, {"5"," "," " }, {"5"," "," " }, { "5","5","5" }, { " "," ", "5" }, { " ", " ", "5" }, { "5","5","5" } };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(five[h, j]);
                        }
                        Console.WriteLine();
                    }
                    y = 5;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '6':
                    n = Convert.ToInt32(s[i]);
                    string[,] six = new string[7, 3] { { "6","6","6" }, {"6", " "," " }, {"6", " ", " " }, {"6","6","6" }, {"6"," ","6"}, {"6"," ","6" }, {"6","6","6" } };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(six[h, j]);
                        }
                        Console.WriteLine();
                    }
                    x = 6;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '7':
                    n = Convert.ToInt32(s[i]);
                    string[,] seven = new string[7,4] { { "7","7","7"," " }, { " ", " ", "7"," " }, { " ", " ", "7" ," "}, { " "," ","7"," " }, { "7","7","7","7" }, { " ", " ", "7"," " }, { " ", " ","7"," " } };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 4; j++)
                        {
                            Console.Write(seven[h, j]);
                        }
                        Console.WriteLine();
                    }
                    y = 7;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '8':
                    n = Convert.ToInt32(s[i]);
                    string[,] eight = new string [7,3 ] { {"8","8","8" }, {"8"," ","8" }, { "8"," ","8" }, { "8","8","8" }, { "8"," ","8" }, { "8"," ","8" }, {"8","8","8"} };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(eight[h, j]);
                        }
                        Console.WriteLine();
                    }
                    x = 8;
                     sum1[q]=n;
                   q = q + 1;
                    break;
                case '9':
                    n = Convert.ToInt32(s[i]);
                    string[,] nine = new string[7, 3] { {"9","9","9" }, {"9"," ","9" }, {"9"," ","9" }, {"9","9","9"}, {" "," ","9"}, {" "," ","9" }, {"9","9","9"} };
                    for (int h = 0; h < 7; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(nine[h, j]);
                        }
                        Console.WriteLine();
                    }
                    y = 9;
                     sum1[q]=n;
                   q = q + 1;
                    break;
              
                case '+':
                 string[,] plus = new string [3,3] {{" " ,"+"," " },{"+" ,"+","+"},{" ","+"," "}};
                 for (int h = 0; h < 3; h++)
                 {
                     for (int j = 0; j < 3; j++)
                     {
                         Console.Write(plus[h, j]);
                     }
                     Console.WriteLine();
                 }
                

                    break;
                case '-':
                    string[,] min = new string[3, 3] { { " ", " ", " " }, { "-", "-", "-" }, { " ", " ", " " } };
                    for (int h = 0; h < 3; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(min[h, j]);
                        }
                        Console.WriteLine();
                        c = '-';
                    }
                    break;
                case '*':
                   string[,] mul = new string[3, 3] { { "*", " ", "*" }, { " ", "*", " " }, { "*", " ", "*" } };
                   for (int h = 0; h < 3; h++)
                   {
                       for (int j = 0; j < 3; j++)
                       {
                           Console.Write(mul[h, j]);
                       }
                       Console.WriteLine();
                   }
                    break;
                case '/':
                    string[,] div = new string[3, 3] { { " ", " ", "/" }, { " ", "/", " " }, { "/", " ", " " } };
                    for (int h = 0; h < 3; h++)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Console.Write(div[h, j]);
                        }
                        Console.WriteLine();
                    }
                    break;
                default :
                   
                    break;

            }

        }
        string[,] equel = new string[3, 3] { { "=", "=", "= " }, { " ", " ", " " }, { "=", "=", "=" } };
        for (int h = 0; h < 3; h++)
        {
            for (int j = 0; j < 3; j++)
            {
                Console.Write(equel[h, j]);
            }
            Console.WriteLine();
        }

//here i must extract two number and put their sum in f.problem is here.
      
        switch(s[1])
        {
            case'+':
               f = a+b;
                break;
            case'-':
                f = a - b;
                break;
            case'*':
                f = a * b;
                break;
            case'/':
                f = a / b;
                break;
        }
        
        switch(f)
        {

            case 0:
               
                string[,] zero = new string[7, 3] { { "0", "0", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", " ", "0" }, { "0", "0", "0" }, { "0", "0", "0" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(zero[h, j]);
                    }
                    Console.WriteLine();

                }
               



                break;
            case 1:
                
                string[,] one = new string[7, 3] { { "1", "1", "1" }, { " ", " ", "1" }, { " ", " ", "1" }, { " ", " ", "1" }, { " ", " ", "1" }, { " ", " ", "1" }, { " ", " ", "1" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(one[h, j]);
                    }
                    Console.WriteLine();

                }

               
                break;
            case  2:
               
                string[,] two = new string[7, 3] { { "2", "2", "2" }, { " ", " ", "2" }, { " ", " ", "2" }, { "2", "2", "2" }, { "2", " ", " " }, { "2", " ", " " }, { "2", "2", "2" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(two[h, j]);
                    }
                    Console.WriteLine();
                }
               
                break;
            case 3:
               
                string[,] three = new string[7, 3] { { "3", "3", "3" }, { " ", " ", "3" }, { " ", " ", "3" }, { "3", "3", "3" }, { " ", " ", "3" }, { " ", " ", "3" }, { "3", "3", "3" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(three[h, j]);
                    }
                    Console.WriteLine();
                }
              

                break;
            case 4:
            
                string[,] four = new string[7, 3] { { "4", " ", "4" }, { "4", " ", "4" }, { "4", " ", "4" }, { "4", "4", "4" }, { " ", " ", "4" }, { " ", " ", "4" }, { " ", " ", "4" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(four[h, j]);
                    }
                    Console.WriteLine();
                }
             
                break;
            case 5:
              
                string[,] five = new string[7, 3] { { "5", "5", "5" }, { "5", " ", " " }, { "5", " ", " " }, { "5", "5", "5" }, { " ", " ", "5" }, { " ", " ", "5" }, { "5", "5", "5" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(five[h, j]);
                    }
                    Console.WriteLine();
                }
               
                break;
            case 6:
              
                string[,] six = new string[7, 3] { { "6", "6", "6" }, { "6", " ", " " }, { "6", " ", " " }, { "6", "6", "6" }, { "6", " ", "6" }, { "6", " ", "6" }, { "6", "6", "6" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(six[h, j]);
                    }
                    Console.WriteLine();
                }
                
                break;
            case 7:
               
                string[,] seven = new string[7, 4] { { "7", "7", "7", " " }, { " ", " ", "7", " " }, { " ", " ", "7", " " }, { " ", " ", "7", " " }, { "7", "7", "7", "7" }, { " ", " ", "7", " " }, { " ", " ", "7", " " } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        Console.Write(seven[h, j]);
                    }
                    Console.WriteLine();
                }
                
                break;
            case 8:
            
                string[,] eight = new string[7, 3] { { "8", "8", "8" }, { "8", " ", "8" }, { "8", " ", "8" }, { "8", "8", "8" }, { "8", " ", "8" }, { "8", " ", "8" }, { "8", "8", "8" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(eight[h, j]);
                    }
                    Console.WriteLine();
                }
               
                break;
            case 9:
               
                string[,] nine = new string[7, 3] { { "9", "9", "9" }, { "9", " ", "9" }, { "9", " ", "9" }, { "9", "9", "9" }, { " ", " ", "9" }, { " ", " ", "9" }, { "9", "9", "9" } };
                for (int h = 0; h < 7; h++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        Console.Write(nine[h, j]);
                    }
                    Console.WriteLine();
                }

                break; 
           
        }





        Console.ReadKey();
    }

}

推荐答案

Short answer: scan it.

I.e. split the text into a stream of tokens and then process one token after the other (e.g. to store them in a list).



Before scanning, define what the tokens are, e.g. numbers and operators.

- What exactly is a number? Only one digit or multiple digits in sequence?

- What operators are supported?

- Any white spaces allowed between the tokens? Any comments supported?

- ...?



Depending on the answers above, you may chose different approaches.



The very simplest token definition is

- Number = 0...9 // each number consists of one digit only

- Operator = + // only the plus operator supported

- no spaces

- no comments

Short answer: scan it.
I.e. split the text into a stream of tokens and then process one token after the other (e.g. to store them in a list).

Before scanning, define what the tokens are, e.g. numbers and operators.
- What exactly is a number? Only one digit or multiple digits in sequence?
- What operators are supported?
- Any white spaces allowed between the tokens? Any comments supported?
- ...?

Depending on the answers above, you may chose different approaches.

The very simplest token definition is
- Number = 0...9 // each number consists of one digit only
- Operator = + // only the plus operator supported
- no spaces
- no comments
public class Scanner
{
    private string _text;
    public Scanner(string text) { _text = text; }
    public IEnumerator<string> GetEnumerator()
    {
        foreach(char c in _text)
        {
            string s = null;                       // default = error
            if (char.IsDigit(c)) s = c.ToString(); // number = one digit
            else if (c == '+') s = c.ToString();   // operator = +
            yield return s;
        }   
    }
}

Using by token wise forwarding:

Using by token wise forwarding:

string text = "2+4";
var tokens = new Scanner(text).GetEnumerator();
while(tokens.MoveNext())
{
    string token = tokens.Current;
    if (token == null) Console.WriteLine("Error");
    else               Console.WriteLine(token);
}

Alternatively using by iterator:

Alternatively using by iterator:

string text = "2+4";
foreach (string token in new Scanner(text))
{
    if (token == null) Console.WriteLine("Error");
    else               Console.WriteLine(token);
}



This scanner class is a bit overkill for the simple definition. But you have now a framework to extend to more complex tokens. Eventually, you will switch to Regex tokenizing.

Cheers

Andi


This scanner class is a bit overkill for the simple definition. But you have now a framework to extend to more complex tokens. Eventually, you will switch to Regex tokenizing.
Cheers
Andi


Try Regex:

Try Regex:
using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string str = "s=2+4+76";
        Match match = Regex.Match(str, @"\d+");
        while (match.Success)
        {
            Console.WriteLine(match.Value);
            match = match.NextMatch();
        }
    }
}


Parse it into elements, using string manipulations and/or Regex (https://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx[^], https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28v=vs.110%29.aspx[^]). Then use methods int.TryParse (uint.TryParse, long.TryParse, whatever types you need); see, for example, https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx[^].



The detail of parsing depends on the format of your strings, which you did not bother to share with us. An example is not spec, but it’s possible that you need to make a fully-fledged parser of some kins. Please see, for example, http://www.codeproject.com/search.aspx?q=%22C%23%22+expression+parser&doctypeid=1[^].



—SA
Parse it into elements, using string manipulations and/or Regex (https://msdn.microsoft.com/en-us/library/system.string%28v=vs.110%29.aspx[^], https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex%28v=vs.110%29.aspx[^]). Then use methods int.TryParse (uint.TryParse, long.TryParse, whatever types you need); see, for example, https://msdn.microsoft.com/en-us/library/system.int32.tryparse%28v=vs.110%29.aspx[^].

The detail of parsing depends on the format of your strings, which you did not bother to share with us. An example is not spec, but it's possible that you need to make a fully-fledged parser of some kins. Please see, for example, http://www.codeproject.com/search.aspx?q=%22C%23%22+expression+parser&doctypeid=1[^].

—SA


这篇关于我怎样才能从字符串中获取数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆