C# String Theory—String static methods

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
CSharp-Online.NET:Articles
C# Articles

C# String Theory

edit
This page is under construction and not in a completed state. If you do not find here that which you seek, perform a search. For help with various kinds of searches, consult Searching. Image:UnderConstruction01.png

String static methods

Contents


Compare method

The Compare method compares two designated String objects using the current culture to obtain culture-specific information, e.g., alphabetic order, casing rules. For example, a culture might specify that certain character combinations should be treated as a single character, or that uppercase and lowercase characters be compared in a certain way, or that the character sorting order depends on preceding or succeeding characters.

Compare returns a 32-bit signed integer indicating the lexical relationship between the two comparands as follows:

Value Meaning
Less than zero stringA is less than stringB.
Zero stringA equals stringB.
Greater than zero stringA is greater than stringB.

Comparison terminates when an inequality is detected or when both strings have been compared. But, if the two strings compare equal to the end of one string, and the other string has remaining characters, then the string with characters remaining is considered the greater.

One or both comparands can be null. Any string—including the empty string ("")—compares greater than a null reference. Two null references compare equal.

string stringA = "abcdef";
string stringB = "Abcdef";
    
int comparison = string.Compare (stringA, stringB, false);
if (comparison < 0) {
   Console.WriteLine ("stringB greater than stringA");
   } 
if (comparison == 0) { 
   Console.WriteLine ("Strings equal");
   } 
if (comparison > 0)  {
   Console.WriteLine ("stringB less than stringA");
   } 
// Case sensitive comparison: stringB greater than stringA"" 

The Compare method is overloaded to allow for case sensitive and insensitive comparison, to factor in cultural information, and to specify string positions where the comparison begins, and length of comparison.

CompareOrdinal Method

The CompareOrdinal Method compares two String objects by evaluating the numeric values of the corresponding char objects in each string.

String stringA = "A";
String stringa = "a";
int result = String.CompareOrdinal (stringA, stringa);
Console.Write ("String '{0}' {1} string '{2}'", stringA, 
   ((result < 0) ? "less than" : ((result > 0) 
      ? "greater than" : "equal to")), stringa);

Concat Method

Copy Method

Format Method

Join Method

ReferenceEquals Method

Previous_Page_.gif Next_Page_.gif

Personal tools