0

searching on the net I found that if a string contains "spaces" then that string is not an empty string!

So:

string a = "";
string b= "     ";

if(a==b)
{
    MessageBox.Show("I want them to be the same");
}
else
{
    MessageBox.Show("They are not the same thing!");
}

My question now is:

1) How can I tell that I want to make a="" AND b=" " are the same thing?

2) Why when I fill the string b from the DATABASE it adds me this "spaces" meanwhile in the DATABASE is just an empty cell?

DavidG
  • 113,891
  • 12
  • 217
  • 223
  • 3
    1) `string.Trim` is your friend. 2) Depends on your database and how you get the value from there. How do you see "it adds spaces"? – MakePeaceGreatAgain Jun 12 '19 at 10:20
  • Can you post Docs? – Olivero SRL Jun 12 '19 at 10:21
  • 8
    @HimBromBeere I'd more likely go with `string.IsNullOrWhiteSpace` than create new strings. – DavidG Jun 12 '19 at 10:22
  • 3
    @OliveroSRL Your database is probably adding trailing spaces because your columns are `char(50)` instead of `varchar(50)`. – DavidG Jun 12 '19 at 10:22
  • @HimBromBeere i can see the "spaces" when i debug and look inside the datatable rows And DavidG my Column in the DB is nchar(10) – Olivero SRL Jun 12 '19 at 10:26
  • You have two questions here, which one do you want to be answered? You can only ask one thing at a time. – DavidG Jun 12 '19 at 10:27
  • Ah yeah sorry guys i just want to know how to tell visual that these 2 string are equal. – Olivero SRL Jun 12 '19 at 10:29
  • @DavidG Well, OP wants to compare two values, not just check if one is a whitespace (or more). – MakePeaceGreatAgain Jun 12 '19 at 10:30
  • @HimBromBeere Yes, but I'd write the logic to do that check before the equality check. Though I'd actually fix the database so this isn't an issue in the first place. – DavidG Jun 12 '19 at 10:32
  • 1
    Olivero, you seem to be missing a crucial point here: they _are not equal_. And they never will be. Therefore, you need to either manipulate the strings so that they become equal, or perform custom logical tests that define your version of equality. – DonBoitnott Jun 12 '19 at 11:02
  • because you use == to compare string , I also advice you to check this topic https://stackoverflow.com/questions/144530/or-equals –  Jun 12 '19 at 11:09

1 Answers1

1

No,

A string, wich is declared as an string.Empty or "" aren't the same as a string with " ". Whitespace is a character and with it inside of a string, it isn't null or empty anymore.

You can check it:

this one tells if its Null or Empty ("")

string a;
string b = string.Empty;
string c = "";
string d = "     ";

if(string.IsNullOrEmpty(HERE))
{
// a b and c will be true, d will be false
}

if(string.IsNullOrWhiteSpace(HERE))
{
// a b c and d will be true.
}

So to answer your questions: 1) you never can because it isn't the same thing. You can trim temporarely the whitespace to make it empty. Like:

var temp = StringWithWhiteSpace.Replace(" ", string.Empty);

and then check if it is Null or Empty and work with the temp string.

if(string.IsNullOrEmpty(temp))
{
}

or check directly for string.IsNullOrWhiteSpace()

2) it means that in your database there are spaces and it is not an empty cell. You can delete all whitespace Cells in your database if you use

UPDATE tableName SET columnName='' WHERE columnName='   ';

if every "empty" cell has the same numbers of whitespaces. if not you can look here: SQL query - filter out field containing only spaces

LoaStaub
  • 63
  • 14