C # Compare string and string common functions

Source: Internet
Author: User

C # Comparing string and string Common functions Example August 26, 2007 Sunday 02:29 There are several ways to compare strings in C #, such as:

1. String.Compare (x, y);
2. String. Equals (x, y);

If the comparison is not case-sensitive, the corresponding is:
String.Compare (x, y);
String. Equals (x, y);


Note: String.Compare The meaning of the comparison result:

Value
Meaning

Less than 0
X is less than Y. or x is a null reference (Nothing in Visual Basic).

Zero
x equals Y.

Greater than 0
X is greater than Y. Or y is a null reference (Nothing in Visual Basic).


String. The meaning of the equals comparison result is:
Value
Meaning

True
x equals Y.

False
x does not equal Y.


--------------------------------------------------------------------------------


Other commonly used string operations:

1. Extracting substrings from a string
The StringBuilder class does not have a method to support substrings, so it must be extracted with the string class.
String mystring= "My name is Ynn.";
Displays "name is Ynn."
Console.WriteLine (mystring. Substring (3));
Displays "Ynn"
Console.WriteLine (mystring. Substring (11,3));


2. Comparing strings
There are four methods of the String class: Compare (), CompareTo (), compareordinal (), Equals ().
The Compare () method is a static version of the CompareTo () method. As long as the "=" operator is used, the Equals () method is called, and the Equals () method is equivalent to "=". The CompareOrdinal () method compares two strings to the local language and file.
Example:
int result;
BOOL Bresult;
S1= "AAAA";
S2= "BBBB";
Compare () method
The result value is "0", etc., less than 0 means S1 < S2, greater than 0 means 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 two strings are built-in and equal, and the static method is much faster.

3. String formatting

3.1 Formatting numbers
Format character descriptions and associated properties

--------------------------------------------------------------------------------

c, C currency format.
D, d decimal format.
E, E scientific count (exponential) format.
F, f fixed point format.
G, g regular format.
N, n number format.
R, R round-trip format, which ensures that a number converted to a string has the same value as the original number when it is converted back to a number.
x, x hexadecimal format.

--------------------------------------------------------------------------------

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%
The default formatting puts a space between the numbers and the percent signs. The customization method is as follows:
Where the NumberFormatInfo class is a member of the System.Globalization namespace, the namespace must be imported into 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.3%;
Console.WriteLine (Val. ToString ("P1", MyFormat)); Displays 12.3%;
Formatting has a lot of flexibility. The following example shows 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= "_"; Separator
Mycurrency. Currencygroupsizes=groupsize;
Output #1_234_5_67:89
Console.WriteLine (Val. ToString ("C", mycurrency));


3.2 Formatting Dates
The output form depends on the culture 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", "D DDD, 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, July 2004
F:sunday, July 2004 10:52
F:sunday, July 2004 10:52:36
g:07/11/2004 10:52
g:07/11/2004 10:52:36
M:july 11
R:sun, 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, 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 Formatting enumerations
Enum Classmen
{
Ynn=1,
yly=2,
Css=3,
C++=4
}
Get the enumeration string information as follows:
Classmen myclassmen=classmen.yly;
Console.WriteLine (Myclassmen.     ToString ()); Displays yly
Console.WriteLine (Myclassmen. ToString ("D")); Displays 2
To get the text from the system enumeration, the person information is as follows:
DayOfWeek Day=dayofweek.friday;
Displays "Day is Friday"
Console.WriteLine (String.Format ("Day was {0:g}", day));
The formatted string "G" displays the enumeration as a string.

C # Compare string and string common functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.