0

Guys need help with this one.

Here's the condition / algorithm.

"If first digit is number/int and last digit is letter add two zero in front of the string"

Hoping for any idea!

Thanks.

5
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by Mitja Bonca
Featured Replies
  • Check This code [CODE] private void Form1_Load(object sender, EventArgs e) { String s = "12shshasj"; if (isNumeric(s.ElementAt(0).ToString())) MessageBox.Show(s.ElementAt(0) + " is numeric"); if(!isNumeric(s.ElementAt(s.Length-1).ToString())) MessageBox.Show(s.ElementAt(s.Length - 1) + " is not numeric"); } public bool isNumeric(string val) { double Num; bool isNum = double.TryParse(val, out Num); return isNum; }[/CODE] Read More

0

First of all, that kind of expressions you formed in text are "regular expressions".

RegEx Howto

Another way would be to take one character left of the string and try to cast it into an int for example. If you get an exception you know it's not a number. Same on the right side but if you get an exception you know you're right.

1

Check This code

private void Form1_Load(object sender, EventArgs e)
        {
            String s = "12shshasj";
            if (isNumeric(s.ElementAt(0).ToString()))
                MessageBox.Show(s.ElementAt(0) + " is numeric");
            if(!isNumeric(s.ElementAt(s.Length-1).ToString()))
                MessageBox.Show(s.ElementAt(s.Length - 1) + " is not numeric");
        }
        public bool isNumeric(string val)
        {
            double Num;
            bool isNum = double.TryParse(val, out Num);
            return isNum;
        }

Edited by abelLazm: n/a

0

That should be pretty much it, if it works.

Little advice: Visual Basic has a function called isNumeric. You can use the Visual Basic Library (have to import it), but your way is the easy one.

ps: Give positive permlinks for resolutions! :-D

Edited by Wegginho: fun

0

But dear This is C# ... Actually i have pasted code from one of my project where i have written a seperate code but it can be written as

private void Form1_Load(object sender, EventArgs e)
        {
            String s = "12shshasj";
           double Num;
            if (double.TryParse(s.ElementAt(0).ToString(), out Num))
                MessageBox.Show(s.ElementAt(0) + " is numeric");
            if(!double.TryParse(s.ElementAt(s.Length-1).ToString(), out Num))
                MessageBox.Show(s.ElementAt(s.Length - 1) + " is not numeric");
        }

Edited by abelLazm: n/a

0

Try this:

String s = "12shshasj";
            String newStr = String.Empty;
            if (char.IsDigit(s,0) && char.IsLetter(s,s.Length-1))
            {
                newStr = s.Insert(0, "00");
            }
0

@abelLazm
The OP wanted inserting, so your line 3 will do the job equally well. :)
BTW. did you know that the C# compiler translates a + here, to a call to String.Concat?

0

My part:

string value ="some value";
 if (char.IsDigit(value[0]) && char.IsLetter(value[value.Length - 1]))
       value = "00" + value;

Edited by Mitja Bonca: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.