C # how to compare two strings that have matching letters but they have spaces
Here is my code below, how can I make if
return true as it is currently missing a statement if
because there is a space in the string values.
string instrucType = "FM";
string tenInstrucType = "FM ";
if (tenInstrucType.Equals(instrucType))
{
blLandlordContactNumberHeader.Visible = true;
lblLandlordContactNumber.Text = landlordData.LandlordContact.DefPhone;
lblLandlordEmailHeader.Visible = true;
lblLandlordEmail.Text = landlordData.LandlordContact.DefEmail;
}
+3
source to share
5 answers
If the space is only at the end of the line, trim both lines:
if (tenInstrucType.Trim().Equals(instrucType.Trim()))
If you want to ignore all whitespace characters, you can remove them from the string:
string normalized1 = Regex.Replace(tenInstrucType, @"\s", "");
string normalized2 = Regex.Replace(instrucType, @"\s", "");
if (normalized1 == normalized2) // note: you may use == and Equals(), as you like
{
// ....
}
+1
source to share