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.

Recommended Answers

All 10 Replies

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.

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

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

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

Ok tnx im going to check if its working.

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

ddanbe appending in a string isn't easy by

s+="00";
//or
s="00"+s;

@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?

yes i knew that :)

My part:

string value ="some value";
 if (char.IsDigit(value[0]) && char.IsLetter(value[value.Length - 1]))
       value = "00" + value;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.