There are multiple methods to compare strings with C #, such:
1. string. Compare (x, y );
2. string. Equals (x, y );
If it is case-insensitive, the result is:
String. Compare (x, y );
String. Equals (x, y );
Note: The meaning of the comparison result of string. Compare:
Value |
Description |
Less Than Zero |
X is less than y. Or if x is null reference (Nothing in Visual Basic ). |
Zero |
X equals to y. |
Greater than zero |
X is greater than y. Or y is a null reference (Nothing in Visual Basic ). |
The comparison result of string. Equals is as follows:
Value |
Description |
True |
X equals to y. |
False |
X is not equal to y. |
Other common string operations:
1. Extract substrings from strings
The StringBuilder class does not support substrings. Therefore, the String class must be used for extraction.
String mystring = "My name is ynn .";
// Displays "name is ynn ."
Console. WriteLine (mystring. Substring (3 ));
// Displays "ynn"
Console. WriteLine (mystring. Substring (11,3 ));
2. compare strings
The String class has four methods: Compare (), CompareTo (), CompareOrdinal (), and Equals ().
The Compare () method is the static version of The CompareTo () method. If the "=" operator is used, the Equals () method is called. The Equals () method is equivalent to "=. The CompareOrdinal () method does not compare two strings with local languages and files.
Example:
Int result;
Bool bresult;
S1 = "aaaa ";
S2 = "bbbb ";
// Compare () method
// The result value is "0", indicating equality. If it is less than zero, it indicates s1 <s2, and if it is greater than zero, it indicates s1> s2.
Result = String. Compare (s1, s2 );
Result = s1.CompareTo (s2 );
Result = String. CompareOrdinal (s1, s2 );
Bresult = s1.Equals (s2 );
Bresult = String. Equals (s1, s2 );
One exception is that both strings are built-in and equal, and the static method is much faster.
3. String formatting
3.1 format a number
Description of format characters and associated attributes
C and C currency formats.
In decimal format.
E, E scientific count (INDEX) format.
F and F are fixed points.
G.
N, N numeric format.
The r and R round-trip formats Ensure that the number converted to a string has the same value as the original number when being converted back to a number.
X and X hexadecimal formats.
Double val = Math. PI;
Console. WriteLine (val. ToString (); // displays 3.14159265358979
Console. WriteLine (val. ToString ("E"); // displays 3.141593E + 000
Console. WriteLine (val. ToString ("F3"); // displays 3.142
Int val = 65535;
Console. WriteLine (val. ToString ("x"); // displays ffff
Console. WriteLine (val. ToString ("X"); // displays FFFF
Single val = 0.123F;
Console. WriteLine (val. ToString ("p"); // displays 12.30%
Console. WriteLine (val. ToString ("p1"); // displays 12.3%
By default, a space is placed between the digit and the percent sign. The customization method is as follows:
The NumberFormatInfo class is a member of the System. Globalization namespace. Therefore, the namespace must be imported to the program.
Single val = 0.123F;
Object myobj = NumberFormatInfo. CurrentInfo. Clone () as NumberFormatInfo;
NumberFormatInfo myformat = myobj as NumberFormatInfo;
Myformat. PercentPositivePattern = 1;
Console. WriteLine (val. ToString ("p", myformat); // displays 12.30%;
Console. WriteLine (val. ToString ("p1", myformat); // displays 12.3%;
Formatting is flexible. The following example demonstrates a meaningless currency structure:
Double val = 1234567.89;
Int [] groupsize = {2, 1, 3 };
Object myobj = NumberFormatInfo. CurrentInfo. Clone ();
NumberFormatInfo mycurrency = myobj as NumberFormatInfo;
Mycurrency. CurrencySymbol = "#"; // symbol
Mycurrency. CurrencyDecimalSeparator = ":"; // decimal point
Mycurrency. CurrencyGroupSeparator = "_"; // delimiter
Mycurrency. CurrencyGroupSizes = groupsize;
// Output #1_234_5_67: 89
Console. WriteLine (val. ToString ("C", mycurrency ));
3.2 format a date
The output format depends on the cultural settings of the user's computer.
Using System;
Using System. Globalization;
Public class MainClass
{
Public static void Main (string [] args)
{
DateTime dt = DateTime. Now;
String [] format = {"d", "D", "f", "F", "g", "G", "m", "r ", "s", "t", "T", "u", "U", "y", "dddd, MMMM dd yyyy", "ddd, MMM d \ "'\" yy "," dddd, MMMM dd "," M/yy "," dd-MM-yy ",};
String date;
For (int I = 0; I <format. Length; I ++)
{
Date = dt. ToString (format [I], DateTimeFormatInfo. InvariantInfo );
Console. WriteLine (String. Concat (format [I], ":", date ));
}
}
}
D: 07/11/2004 <===== output
D: Sunday, 11 July 2004
F: Sunday, 11 July 2004
F: Sunday, 11 July 2004 10:52:36
G: 07/11/2004
G: 07/11/2004 10:52:36
M: July 11
R: Sun, 11 Jul 2004 10:52:36 GMT
S: 2004-07-11T10: 52: 36
T: 10: 52
T: 10: 52: 36
U: 2004-07-11 10: 52: 36Z
U: Sunday, 11 July 2004 02:52:36
Y: 2004 July
Dddd, MMMM dd yyyy: Sunday, July 11 2004
Ddd, MMM d "'" yy: Sun, Jul 11' 04
Dddd, MMMM dd: Sunday, July 11
M/yy: 7/04
Dd-MM-yy: 11-07-04
3.3 format Enumeration
Enum classmen
{
Ynn = 1,
Yly = 2,
Css = 3,
C ++ = 4
}
Obtain the enumerated string information as follows:
Classmen myclassmen = classmen. yly;
Console. WriteLine (myclassmen. ToString (); // displays yly
Console. WriteLine (myclassmen. ToString ("d"); // displays 2
The following information is obtained from the system enumeration:
DayOfWeek day = DayOfWeek. Friday;
// Displays "Day is Friday"
Console. WriteLine (String. Format ("Day is {0: G}", day ));
Format the string "G" to display the enumeration as a string.
There are multiple methods to compare strings with C #, such:
1. string. Compare (x, y );
2. string. Equals (x, y );
If it is case-insensitive, the result is:
String. Compare (x, y );
String. Equals (x, y );
Note: The meaning of the comparison result of string. Compare:
Value |
Description |
Less Than Zero |
X is less than y. Or if x is null reference (Nothing in Visual Basic ). |
Zero |
X equals to y. |
Greater than zero |
X is greater than y. Or y is a null reference (Nothing in Visual Basic ). |
The comparison result of string. Equals is as follows:
Value |
Description |
True |
X equals to y. |
False |
X is not equal to y. |
Other common string operations:
1. Extract substrings from strings
The StringBuilder class does not support substrings. Therefore, the String class must be used for extraction.
String mystring = "My name is ynn .";
// Displays "name is ynn ."
Console. WriteLine (mystring. Substring (3 ));
// Displays "ynn"
Console. WriteLine (mystring. Substring (11,3 ));
2. compare strings
The String class has four methods: Compare (), CompareTo (), CompareOrdinal (), and Equals ().
The Compare () method is the static version of The CompareTo () method. If the "=" operator is used, the Equals () method is called. The Equals () method is equivalent to "=. The CompareOrdinal () method does not compare two strings with local languages and files.
Example:
Int result;
Bool bresult;
S1 = "aaaa ";
S2 = "bbbb ";
// Compare () method
// The result value is "0", indicating equality. If it is less than zero, it indicates s1 <s2, and if it is greater than zero, it indicates s1> s2.
Result = String. Compare (s1, s2 );
Result = s1.CompareTo (s2 );
Result = String. CompareOrdinal (s1, s2 );
Bresult = s1.Equals (s2 );
Bresult = String. Equals (s1, s2 );
One exception is that both strings are built-in and equal, and the static method is much faster.
3. String formatting
3.1 format a number
Description of format characters and associated attributes
C and C currency formats.
In decimal format.
E, E scientific count (INDEX) format.
F and F are fixed points.
G.
N, N numeric format.
The r and R round-trip formats Ensure that the number converted to a string has the same value as the original number when being converted back to a number.
X and X hexadecimal formats.
Double val = Math. PI;
Console. WriteLine (val. ToString (); // displays 3.14159265358979
Console. WriteLine (val. ToString ("E"); // displays 3.141593E + 000
Console. WriteLine (val. ToString ("F3"); // displays 3.142
Int val = 65535;
Console. WriteLine (val. ToString ("x"); // displays ffff
Console. WriteLine (val. ToString ("X"); // displays FFFF
Single val = 0.123F;
Console. WriteLine (val. ToString ("p"); // displays 12.30%
Console. WriteLine (val. ToString ("p1"); // displays 12.3%
By default, a space is placed between the digit and the percent sign. The customization method is as follows:
The NumberFormatInfo class is a member of the System. Globalization namespace. Therefore, the namespace must be imported to the program.
Single val = 0.123F;
Object myobj = NumberFormatInfo. CurrentInfo. Clone () as NumberFormatInfo;
NumberFormatInfo myformat = myobj as NumberFormatInfo;
Myformat. PercentPositivePattern = 1;
Console. WriteLine (val. ToString ("p", myformat); // displays 12.30%;
Console. WriteLine (val. ToString ("p1", myformat); // displays 12.3%;
Formatting is flexible. The following example demonstrates a meaningless currency structure:
Double val = 1234567.89;
Int [] groupsize = {2, 1, 3 };
Object myobj = NumberFormatInfo. CurrentInfo. Clone ();
NumberFormatInfo mycurrency = myobj as NumberFormatInfo;
Mycurrency. CurrencySymbol = "#"; // symbol
Mycurrency. CurrencyDecimalSeparator = ":"; // decimal point
Mycurrency. CurrencyGroupSeparator = "_"; // delimiter
Mycurrency. CurrencyGroupSizes = groupsize;
// Output #1_234_5_67: 89
Console. WriteLine (val. ToString ("C", mycurrency ));
3.2 format a date
The output format depends on the cultural settings of the user's computer.
Using System;
Using System. Globalization;
Public class MainClass
{
Public static void Main (string [] args)
{
DateTime dt = DateTime. Now;
String [] format = {"d", "D", "f", "F", "g", "G", "m", "r ", "s", "t", "T", "u", "U", "y", "dddd, MMMM dd yyyy", "ddd, MMM d \ "'\" yy "," dddd, MMMM dd "," M/yy "," dd-MM-yy ",};
String date;
For (int I = 0; I <format. Length; I ++)
{
Date = dt. ToString (format [I], DateTimeFormatInfo. InvariantInfo );
Console. WriteLine (String. Concat (format [I], ":", date ));
}
}
}
D: 07/11/2004 <===== output
D: Sunday, 11 July 2004
F: Sunday, 11 July 2004
F: Sunday, 11 July 2004 10:52:36
G: 07/11/2004
G: 07/11/2004 10:52:36
M: July 11
R: Sun, 11 Jul 2004 10:52:36 GMT
S: 2004-07-11T10: 52: 36
T: 10: 52
T: 10: 52: 36
U: 2004-07-11 10: 52: 36Z
U: Sunday, 11 July 2004 02:52:36
Y: 2004 July
Dddd, MMMM dd yyyy: Sunday, July 11 2004
Ddd, MMM d "'" yy: Sun, Jul 11' 04
Dddd, MMMM dd: Sunday, July 11
M/yy: 7/04
Dd-MM-yy: 11-07-04
3.3 format Enumeration
Enum classmen
{
Ynn = 1,
Yly = 2,
Css = 3,
C ++ = 4
}
Obtain the enumerated string information as follows:
Classmen myclassmen = classmen. yly;
Console. WriteLine (myclassmen. ToString (); // displays yly
Console. WriteLine (myclassmen. ToString ("d"); // displays 2
The following information is obtained from the system enumeration:
DayOfWeek day = DayOfWeek. Friday;
// Displays "Day is Friday"
Console. WriteLine (String. Format ("Day is {0: G}", day ));
Format the string "G" to display the enumeration as a string.