Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a problem which i have solved but i am searching for better solution.
my problem is i have a string say

string mystr = "hello//abc";

if string contain continuously two or more slash like above string it is invalid string

What I have tried:

C#
private bool isvalidString(string str) 
        {
            int i = 0;
            int lastIndex = -2;
            bool retvalue = true;
            do
            {
                i = str.IndexOf("/", i + 1);
                if (lastIndex + 1 == i)
                {
                    retvalue = false;
                    break;
                }
                lastIndex = i;
                if (i==-1)
                {
                    break;
                }
   
            } while (true);


            return retvalue;
        }
Posted
Updated 25-Oct-16 1:53am

C#
string SomeString = "hello//abc";
           Regex r = new Regex("^[a-zA-Z0-9]*$");
           if (r.IsMatch(SomeString))
           {
               lbl_valid.Text = "Valid";
           }
           else
           {
               lbl_valid.Text = "In-Valid";
           }
 
Share this answer
 
v2
Comments
srilekhamenon 25-Oct-16 7:57am    
solution is not correct it is showing 'hello/abc' as invalid string
Try:
C#
private bool isvalidString(string str) 
    {
    return !str.Contains(@"//");
    }
 
Share this answer
 
Comments
srilekhamenon 25-Oct-16 8:02am    
best solution :)

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