获取字符串中的数字并进行循环 [英] Get number inside of a string and do a loop

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

问题描述

我想得到一个字符串的数字,并将字符串和数字分开,然后,做一个循环并调用一个字符串所说的次数的方法.字符串必须有这样的结构:ABJ3"(只接受一个数字和它之前的 3 个字符)

I want to get a number of a string, and separate the string and the number, and then, do a loop and call a method the number of times the string says. The string has to have this structure: "ABJ3" (Only one number accepted and 3 characters before it)

这是我的代码,但它重复了数百次,我不知道为什么

This is my code, but it repeat hundred of times, I don't know why

            int veces = 0;
            for (int i = 0; i < m.Length; i++)
            {
                if (Char.IsDigit(m[i]))
                    veces = Convert.ToInt32(m[i]);
            }

            if (m.Length == 4)
            {
                for (int i = 0; i <= veces; i++)
                {
                    m = m.Substring(0, 3);
                    operaciones(m, u, t);
                    Thread.Sleep(100);
                }
            }
            operaciones(m,u,t);
            if (u.Length >= 14)
            {
                u = u.Substring(0, 15);
            }

请帮忙?

推荐答案

您现在必须转换您的 m[i] ToString() 您正在将 char 值发送到 Convert.ToInt32,这是一个更高的值 (9= 57 例如)

You have to convert your m[i] ToString() right now you are sending the char value to Convert.ToInt32 and that is a much higher value (9 = 57 for example)

char t = '9';

int te = Convert.ToInt32(t.ToString());

Console.WriteLine(te);

这给我们的结果是 9 但是

This gives us a result of 9 but

char t = '9';

int te = Convert.ToInt32(t);

Console.WriteLine(te);

给我们一个 57 的结果

Gives us a result of 57

所以你需要改变

veces = Convert.ToInt32(m[i]);

veces = Convert.ToInt32(m[i].ToString());

希望对您有所帮助.

最好的问候//KH.

这篇关于获取字符串中的数字并进行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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