Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String compare with special characters in C#

I have two strings "CZSczs" - "ČŽŠčžš" and I want to return true when I compare the strings. I tried with string comparison but it doesn't work.

like image 645
Riddhi Vadukiya Avatar asked May 05 '15 11:05

Riddhi Vadukiya


People also ask

Can I use == to compare strings in C?

In C, string values (including string literals) are represented as arrays of char followed by a 0 terminator, and you cannot use the == operator to compare array contents; the language simply doesn't define the operation.

How do you find the special characters in a string?

To check if a string contains special characters, call the test() method on a regular expression that matches any special character. The test method will return true if the string contains at least 1 special character and false otherwise. Copied!

How do you compare string elements with characters?

strcmp() in C/C++ It compares strings lexicographically which means it compares both the strings character by character. It starts comparing the very first character of strings until the characters of both strings are equal or NULL character is found.

Can string contains special characters in C?

Explanation: Given string contains only special characters. Therefore, the output is Yes.


1 Answers

You can use

int result string.Compare("CZSczs", "ČŽŠčžš", CultureInfo.InvariantCulture, CompareOptions.IgnoreNonSpace); 
bool equal = result == 0;

As pointed out in this question's accepted answer.

like image 177
Tim Schmelter Avatar answered Oct 19 '22 23:10

Tim Schmelter