2

I am currently trying to make an if statement to validate a string. This is currently what I have.

Console.Write("Please enter your phone number: ");
string input = Console.ReadLine();

if (input.Length < 9)
{
    Console.WriteLine("Phone number is not valid, please try again.");
}

string everythingButThePlus = input.Substring(1);
string justThePlus = input.Substring(0, 1);

if (justThePlus = "+" || "1" || "2" || "3" || "4" || "5" || "6" || "7" || "8" || "9" || "0") ;
{

}
Console.ReadLine();

The portion, "justThePlus = "+" ||" is currently got a red underline with the description being,

"Operator '||' cannot be applied to operands of type 'string' and 'string'.

If I can't use OR statements, what is an alternative that works with strings?

1
  • 1
    You should read about the basics of programming. Commented May 18, 2015 at 13:34

9 Answers 9

12

You're almost there:

if (justThePlus == "+" || justThePlus =="1" || justThePlus =="2")

Other issues:

  1. Double == sign
  2. remove ; at the end of the if statement

To improve readability:

string[] allowedValues = { "+", "1", "2" };
if (allowedValues.Contains(justThePlus)) {
Sign up to request clarification or add additional context in comments.

Comments

6

OR statements need a condition either side:

if (justThePlus == "+" || justThePlus == "1" || ....)

Also:

  • Use == instead of = for string comparison
  • Remove semi-colon from the end of your if statement.

Comments

6

You can use an array contains:

if (new[] { "+" ,  "1" ,  "2" ,  "3" ,  "4" ,  "5" ,  "6" , "7" ,  "8" ,  "9" , "0" }.Contains(justThePlus));
{

}

Comments

3

This is a different logic to achieve the same.

var validChars= "+1234567890" ;
if(justThePlus.Length==1 && validChars.IndexOf(justThePlus)>=0)
{
}

1 Comment

var validChars = "+0123456789".ToCharArray(); / Array.BinarySearch(validChars, justThePlus);
3

I think you are making this way too hard.

// Ensures the string has 9 digits and optionally starts with a "+"
Regex regex = new Regex(@"^(\+)?([0-9]{9})");
string input;

while (!regex.IsMatch(input))
{
    Console.Write("Please enter your phone number: ");
    input = Console.ReadLine();
}

Comments

2

|| operator expects both operand as a bool value. This operand performs logical-OR between both operands.

As you can see, using this operator with string operands is meaningless.

You can put your all numbers and + sign in a List<string> and you can check it with Contains and Any methods like;

var listOfNumbersAndPlus = new List<string>(){"+", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"};
if(listOfNumbersAndPlus.Any(justThePlus.Contains))

Comments

2

You can use Regex to validate input.

var input = "+12345678";
var pattern = @"\+\d{8}"; // matches number, which is at leas 8-digit long and starts with '+' sign
var isValidNumber = Regex.IsMatch(input, pattern);

Comments

2

Phone numbers can be represented by user in many different formats, e.g.:

 +1(555)123-45-67
  8 555 123 45 67
     +75551234567 

So I strongly suggest you using regular expressions; in your case it could be

https://www.safaribooksonline.com/library/view/regular-expressions-cookbook/9781449327453/ch04s03.html

For instance

  Console.Write("Please enter your phone number: ");
  string input = Console.ReadLine();

  //TODO: you may want to update/change this pattern
  String pattern = "^\+(?:[0-9] ?){6,14}[0-9]$";

  Boolean isNumberValid = Regex.IsMatch(input, pattern); 

Then, if input string is a valid one, you can just collect all digits within it with a help of Linq:

  // Standard phone number representation: e.g. 155512345678
  String stdNumber = new String(input.Where(ch => ch >= '0' && ch <= '9').ToArray());

Comments

2

Your question has been answered directly (justThePlus == "+" || justThePlus =="1"...). Here's a more efficient way to do the same thing:

    switch (justThePlus)
    {
        case "+" :
        case "0" :
        case "1" :
        case "2" :
        case "3" :
        case "4" :
        case "5" :
        case "6" :
        case "7" :
        case "8" :
        case "9" :
            // ???
            break;

        default :
            // ???
            break;

    }

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.