如何将字符串转换为int并计数?

时间:2012-07-18 12:16:08

标签: c# string-conversion

public void Save(string path , string fileName , PictureBox pb)                
{
    int framesNumberX = 0;
    int framesNumberY = 0;
    string fn;
    string t = Path.GetFileNameWithoutExtension(this.name);
    if (File.Exists(path + "\\" + "DATABASE" + "\\" + fileName + "\\" + t + ".txt"))
    {
        try
        {
            string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + fileName);
            File.Delete(f);
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Could not delete file from disk. Original error: " + ex.Message);
        } 
        fn = path + "\\" + "DATABASE" + "\\" + t + "\\" + fileName;
    }
    else
    {
        fn = path + "\\" + "DATABASE" + "\\" + fileName + "\\" + this.name + ".txt";
    }
    OptionsFile setting_file = new OptionsFile(fn);
    setting_file.SetKey("File Name", fn);
    setting_file.SetKey("Object Name", fileName);
    setting_file.SetKey("Animation Name", this.name);
    setting_file.SetKey("picturebox.Width", pb.Width.ToString());
    setting_file.SetKey("picturebox.Height", pb.Height.ToString());
    string[] xFrames = new string[wocl.Count];
    string[] yFrames = new string[wocl.Count];
    string X="";
    string Y="";
    for (int i = 0; i < wocl.Count; i++)
    {
        X  = string.Format("Frame_X_{0} ", i + 1);
        Y  = string.Format("Frame_Y_{0} ", i + 1);
        int c = Convert.ToInt32(X);
        int d = Convert.ToInt32(Y);
        framesNumberX += c;
        framesNumberY += d;
        for (int j = 0; j < wocl[i].Point_X.Count; j++)
        {
            xFrames[i] += string.Format("{0},", wocl[i].Point_X[j]);
            yFrames[i] += string.Format("{0},", wocl[i].Point_Y[j]);   
        }    
        string tt = xFrames[i].Trim(",".ToCharArray());
        string yy =  yFrames[i].Trim(",".ToCharArray());
        setting_file.SetKey(X, tt);
        setting_file.SetKey(Y, yy);
    }    
    setting_file.SetKey("Number Of Frames X", framesNumberX.ToString());                
}

如果我在循环中做例如:FrameNumber + =;它会计算例如6的帧数。但我想计算X帧出现的次数以及Y的帧数。

我试过这样:

X  = string.Format("Frame_X_{0} ", i + 1);
Y  = string.Format("Frame_Y_{0} ", i + 1);
int c = Convert.ToInt32(X);
int d = Convert.ToInt32(Y);
framesNumberX += c;
framesNumberY += d;

但这不是一个好方法。

也许我需要在变量tt和yy之后最后计算X和Y? 我想计算循环执行变量tt的次数以及变量yy执行了多少次。只需将int变量和make ++放在一起就可以得到我需要将它们分成X和Y的整体帧。所以如果我有6帧,那么X将是3而Y将是3。

2 个答案:

答案 0 :(得分:1)

Convert.Int32()不是Val()方法,换句话说,它不会只取任何字符串中的数字。使用Convert.Int32()时,整个字符串必须是数字,例如:

// doesn't work
string x = "My string 123";
int y = Convert.Int32(x);

// does work
string x = "123";
int y = Convert.Int32(x);

如果我理解正确,你会尝试将某些内容转换为int并同时计算(此处)。

X  = string.Format("Frame_X_{0} ", i + 1); 
Y  = string.Format("Frame_Y_{0} ", i + 1); 
// blows up with an exception
int c = Convert.ToInt32(X); 
int d = Convert.ToInt32(Y); 
framesNumberX += c; 
framesNumberY += d; 

以上内容最好写成:

X  = string.Format("Frame_X_{0} ", i + 1); 
Y  = string.Format("Frame_Y_{0} ", i + 1); 

framesNumberX++; 
framesNumberY++;

// or (depending on what you need)
framesNumberX = i + 1;
framesNumberY = i + 1;

或者 - 哪个更清楚;首先进行数学运算,然后在string.Format()调用中使用结果;

framesNumberX = i + 1;
framesNumberY = i + 1;

X  = string.Format("Frame_X_{0} ", framesNumberX); 
Y  = string.Format("Frame_Y_{0} ", framesNumberY); 

但是,framesNumberX和framesNumberY将彼此等于迭代器i + 1。这就是我开始失去变量目的的地方。如果你能用伪代码澄清你究竟想要完成什么,那将会有所帮助:

public void MyMethod()
{
   ... code here

   // trying to `your explanation`
   ... code here


    // and here, I'm trying to `your explanation`
    ... code here

}

答案 1 :(得分:0)

修改

让此代码正常工作

  X  = string.Format("Frame_X_{0} ", i + 1);
  Y  = string.Format("Frame_Y_{0} ", i + 1);
  int c = Convert.ToInt32(ExtractNumbers(X));//modfied code line
  int d = Convert.ToInt32(ExtractNumbers(Y));//modfied code line
  framesNumberX += c;  framesNumberY += d;

你需要提取类似这样的数字

static string ExtractNumbers( string expr )
{
 return string.Join( null,System.Text.RegularExpressions.Regex.Split( expr, "[^\\d]" ) );
}

上一篇

好的方法是

static void Main()
    {
    // Convert string to number.
    string text = "123";
    int num = -1;
      if( int.TryParse (text,out num))
    Console.WriteLine(num);
    }

检查字符串是否可转换