取字符之间的数字 [英] taking the digits between to characters

查看:75
本文介绍了取字符之间的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在此代码中输入的内容就像50x70CM和6x9INCH一样,当我在检查条件中仅使用"C"或"I"的条件时,我正在使用此代码获取x符号之后以及C或I字符之前的值执行while循环,然后工作正常,但是与代码中的两者一样,它给出了超出范围的错误数组,这是代码中的问题所在...


hello,

my input to this code is just like 50x70CM and 6x9INCH i am using this code for taking the values after x sign and before C or I character when i am checking the condition with only ''C'' or ''I'' by the do while loop then it works fine but With both as in code it gives an error array out of range where is the problem in the code...


string str1 = a;
            char[] strToParse = str1.ToCharArray();
            string height = "";
            char ch;
            int allchar = 0;
            int i = 0;
            do
            {
                ch = strToParse[i];
                i++;
            } while (ch != 'x');
         
            do
            {
                ch = strToParse[allchar];
                allchar++;
            } while ((ch != 'C') || (ch != 'I'));
            allchar--;
            while (i < allchar)
            {
                height += (strToParse[i]).ToString();
                i++;
            }
            int h = Convert.ToInt32(height);
            return h;

推荐答案

尝试一些简单的方法:
Try something a little simpler:
string inputStr = "50x70CM";
string[] parts = inputStr.Split('X', 'x', 'C', 'c', 'I', 'i');
int width = 0;
int height = 0;
if (parts.Length >= 2)
    {
    int.TryParse(parts[0], out width);
    int.TryParse(parts[1], out height);
    }


这篇关于取字符之间的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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