3

Hi so im trying to validate my string here so that it does not allow any input that starts with: "911" so if you type: "9 11", "91 1", "9 1 1" it should go through my if statement. It works with "911" but not the others, here's my code:

using System;
using System.Collections.Generic;

namespace Phone_List
{
    class Program
    {
        static void Main(string[] args)
        {
            var phoneList = new List<string>();
            string input;
            Console.WriteLine("Input: ");

            while ((input = Console.ReadLine()) != "")
            {
                phoneList.Add(input);

                for (int i = 0; i < phoneList.Count; i++)
                {
                    if (phoneList[i].Substring(0, 3) == "911")
                    {
                        input.StartsWith("9 11");
                        input.StartsWith("9 1 1");
                        input.StartsWith("91 1");
                        Console.WriteLine("NO");
                        Console.ReadLine();
                        return;
                    }

                    else
                    {
                        Console.WriteLine("YES");
                        Console.ReadLine();
                        return;
                    }
                }
            }
        }
    }
}

As you can see I am trying to use "input.StartsWith("9 11");" but it does not work...

5
  • That code doesn't really make much sense, you are writing the StartsWith checks inside the condition block when it already started with "911" and nothing else. Also, you have to check if StartsWith returns true, right now, your checks do nothing. Commented Sep 26, 2015 at 17:59
  • 1
    google "Regular Expression". They are constructs designed to validate strings with rules like this. Commented Sep 26, 2015 at 17:59
  • Employ regular expression.Here Regex for all strings starting with 911 would be "911.*" . When there is a match you know the current input starts with 911 Commented Sep 26, 2015 at 18:07
  • You can try to remove the space before validating by using input.Replace(' ', ''); Commented Sep 26, 2015 at 18:08
  • Step through the code and you'll see at least one of the problems. (Another problem is that you are calling StartsWith and throwing away the result.) Commented Sep 26, 2015 at 18:12

4 Answers 4

2

Use regular expressions for checks like this.

For example:

Regex.IsMatch(input, "^\\s*9\\s*1\\s*1");

This regex matches all strings that include whitespaces in front of and between "911".

Sign up to request clarification or add additional context in comments.

2 Comments

I get a: "unrecognize escape sequence" by writing this code.
@Cleon Sorry, forgot to escape the backslash. Normally escape sequences are written like this \s, but \ is a special character in strings, so you have to escape it. This results in \\s. The updated answer is now correct
2

You could use the Replace method of String; the condition you describe can be formulated as follows.

input.Replace(" ", "").StartsWith("911")

1 Comment

So I tried writing this code before my if statement and it still does not work, am I missing somthing?
0

Use the following to check if the string starts with "911":

First create a copy from the input string but without any white spaces:

string input_without_white_spaces =
    new string(input.ToCharArray().Where(x => !char.IsWhiteSpace(x)).ToArray());

Then you can check if the string starts with 911 like this:

if (input_without_white_spaces.StartsWith("911"))
{
    ...
}

Comments

0
bool valid = s.StartsWith("911") || 
            !string.Join("",s.Split()).StartsWith("911");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.