Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...


C#
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;
Posted

1 solution

Try something a little simpler:
C#
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);
    }
 
Share this answer
 
Comments
I.explore.code 16-Oct-12 6:50am    
the simpler the better! +5
Pradeep_kaushik 16-Oct-12 6:59am    
thanx sir.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900