How to check if string character is a space?
Solution 1
See also the String.IsNullOrEmpty
or String.IsNullOrWhiteSpace
.
Solution 2
I'm trying to do a simple check to see whether or not the character in my string is a space;
You can change this code
messArray[i].ToString() != " "
to
char.IsWhiteSpace(messArray[i])
Related videos on Youtube
ninjatogo
Updated on September 15, 2022Comments
-
ninjatogo over 1 year
I've just started programming in C#. I'm trying to build a simple Vigenere text encryption tool as a personal project.
My problem should be very easy to fix, yet it's really stressing me out to find the error. In my code I'm trying to do a simple check to see whether or not the character in my string is a space; I have set up my if statement properly yet it is skipping the first test and moving to the else if, even when the first test is true. Id really like some help on this one.
My problem area is at the bottom.
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class fun2013 { public static void Main() { Console.WriteLine("fun 2013"); string UserName; do { Console.Write("LOGIN//: "); UserName = Console.ReadLine(); } while(UserName != "Max"); Console.WriteLine(("Hello ") + (UserName) + (", enter your key below.")); //USER ENTERS TEXT AT THIS POINT string loweredPass = Console.ReadLine().ToLower(); Console.WriteLine("Changing CASE..."); Console.WriteLine(loweredPass); string Strippedpass = loweredPass.Replace(" ",""); Console.WriteLine("STRIPPING SPACES..."); Console.WriteLine(Strippedpass); int passLength = Strippedpass.Length; Console.WriteLine("Enter your message below."); string userMessage = Console.ReadLine(); int MessageLength = userMessage.Length; //BEGIN PROCESSING STRINGS INTO ARRAYS string temp = ""; StringBuilder bcon = new StringBuilder(); char [] passArray = Strippedpass.ToCharArray(); char [] messArray = userMessage.ToCharArray(); string letterf = ""; for(int i=0, j=0; j < (MessageLength); i++, j++) //i used for key array, j used for message length { >>> if (messArray[i].ToString() == " ") { letterf = " "; } else if (messArray[i].ToString() != " ") { letterf = passArray[i].ToString(); } if (i >= (passLength-1)) //array starts at value 0, length check starts at 1. Subtract 1 to keep them equal {i = -1;} //-1 is used so it will go back to value of 0 on next loop temp = letterf; bcon.Append(temp); } Console.WriteLine(); Console.WriteLine(bcon); Console.WriteLine("Press ENTER to continue..."); Console.ReadLine(); //KILL APPLICATION } }
Thanks for the help everyone, but after further inspection I noticed I made an error in my for loop. I was resetting the message array reader using the same interval as the key array (int i). I changed it to use the correct integer, "j". I also put the "temp" string updater and string builder into each if statement in the loop. It's now running correctly.
for (int i=0, j=0; j < (MessageLength); i++, j++) //i used for key array, j used for message length { if (messArray[j].ToString() != " ") { letterf = passArray[i].ToString(); temp = letterf; bcon.Append(temp); } else if (messArray[j].ToString() == " ") { letterf = " "; temp = letterf; bcon.Append(temp); } if (i >= (passLength-1)) //array starts at value 0, length check starts at 1. Subtract 1 to keep them equal {i = -1;} //-1 is used so it will go back to value of 0 on next loop }
-
oleksii over 10 yearsCan you put
>>>
to show exactly the line where the problem is? -
ninjatogo over 10 yearsThis is still skipping my test for some reason.
-
ninjatogo over 10 yearsSure, it's in the for loop.
-
oleksii over 10 yearsIf I follow correctly, this line
Console.WriteLine(bcon);
would print a white space, but because it's console app you simply don't see it. Can you step through with a Debugger? The comparison should work in either case, I suspect there is a problem in the logic somewhere. -
Black Frog over 10 yearsWhat do you mean skipping? Is it evaluating to false? Or the code is not running at all?
-
ninjatogo over 10 yearsThe statement will run if I change the comparison string to anything but the space character. Otherwise it will take the else if route.
-
ninjatogo over 10 yearsThanks for the help, but what I posted is actually only a small piece of my code which was broken. I will take a look at yours too though, as it is much shorter and looks more functional (handles upper and lower case).
-
WesleyAC almost 4 yearsSo, I see the confusion. The actual code would need to be, e.g., from your code, --> Char.IsWhiteSpace(messArray[i])