Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
237 views
in Technique[技术] by (71.8m points)

c# - Get number inside of a string and do a loop

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);
            }

Some help please?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);

This gives us a result of 9 but

char t = '9';

int te = Convert.ToInt32(t);

Console.WriteLine(te);

Gives us a result of 57

So you need to change

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

to

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

Hope it helped.

Best regards //KH.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...