using System; class Sample { public static void Main() { String str1 = "this is a test"; String str2 = "This is a test"; String str; int result; Console.WriteLine("Honor case:"); result = String.Compare(str1, 2, str2, 2, 2, false); str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to")); Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1); Console.Write("{0} ", str); Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2); } }
Author: coder
Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean), ignore case
using System; class Sample { public static void Main() { String str1 = "this is a test"; String str2 = "This is a test"; String str; int result; result = String.Compare(str1, 2, str2, 2, 2, true); str = ((result < 0) ? "less than" : ((result > 0) ? "greater than" : "equal to")); Console.Write("Substring '{0}' in '{1}' is ", str1.Substring(2, 2), str1); Console.Write("{0} ", str); Console.WriteLine("substring '{0}' in '{1}'.", str2.Substring(2, 2), str2); } }
Use String.Compare to check if a URI is a file URI
using System; public class StringParsing { public static void Main() { } static bool IsFileURI(String path) { return (String.Compare(path, 0, "file:", 0, 5, StringComparison.OrdinalIgnoreCase) == 0); } }
String copy and equal
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace StringManipulation { public class TesterStringCopyEqual { public void Run() { string s1 = "abcd"; string s2 = "ABCD"; // the string copy method string s5 = string.Copy(s2); Console.WriteLine( "s5 copied from s2: {0}", s5); // copy with the overloaded operator string s6 = s5; Console.WriteLine("s6 = s5: {0}", s6); // member method Console.WriteLine( " Does s6.Equals(s5)?: {0}", s6.Equals(s5)); // static method Console.WriteLine( "Does Equals(s6,s5)?: {0}", string.Equals(s6,s5)); // overloaded operator Console.WriteLine( "Does s6==s5?: {0}", s6 == s5); } static void Main() { TesterStringCopyEqual t = new TesterStringCopyEqual(); t.Run(); } } }
Compare strings
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Compare strings. using System; public class CompareDemo { public static void Main() { string str1 = "one"; string str2 = "one"; string str3 = "ONE"; string str4 = "two"; string str5 = "one, too"; if(String.Compare(str1, str2) == 0) Console.WriteLine(str1 + " and " + str2 + " are equal."); else Console.WriteLine(str1 + " and " + str2 + " are not equal."); if(String.Compare(str1, str3) == 0) Console.WriteLine(str1 + " and " + str3 + " are equal."); else Console.WriteLine(str1 + " and " + str3 + " are not equal."); if(String.Compare(str1, str3, true) == 0) Console.WriteLine(str1 + " and " + str3 + " are equal ignoring case."); else Console.WriteLine(str1 + " and " + str3 + " are not equal ignoring case."); if(String.Compare(str1, str5) == 0) Console.WriteLine(str1 + " and " + str5 + " are equal."); else Console.WriteLine(str1 + " and " + str5 + " are not equal."); if(String.Compare(str1, 0, str5, 0, 3) == 0) Console.WriteLine("First part of " + str1 + " and " + str5 + " are equal."); else Console.WriteLine("First part of " + str1 + " and " + str5 + " are not equal."); int result = String.Compare(str1, str4); if(result < 0) Console.WriteLine(str1 + " is less than " + str4); else if(result > 0) Console.WriteLine(str1 + " is greater than " + str4); else Console.WriteLine(str1 + " equals " + str4); } }
Comparing a String to the Beginning or End of a Second String
using System; using System.Data; using System.Text.RegularExpressions; using System.Text; class Class1{ static void Main(string[] args){ string head = "str"; string test = "strVarName"; Console.WriteLine(test.StartsWith(head)); string tail = "Name"; test = "strVarName"; Console.WriteLine(test.EndsWith(tail)); head = "str"; test = "strVarName"; Console.WriteLine(string.Compare(head, 0, test, 0, head.Length, true)); tail = "Name"; test = "strVarName"; Console.WriteLine(string.Compare(tail, 0, test, (test.Length - tail.Length), tail.Length, true)); } }
use the Compare() method to compare strings
using System; class MainClass { public static void Main() { int result; result = String.Compare("bbc", "abc"); Console.WriteLine("String.Compare("bbc", "abc") = " + result); result = String.Compare("abc", "bbc"); Console.WriteLine("String.Compare("abc", "bbc") = " + result); result = String.Compare("bbc", "bbc"); Console.WriteLine("String.Compare("bbc", "bbc") = " + result); result = String.Compare("bbc", "BBC", true); Console.WriteLine("String.Compare("bbc", "BBC", true) = " + result); result = String.Compare("bbc", "BBC", false); Console.WriteLine("String.Compare("bbc", "BBC", false) = " + result); result = String.Compare("Hello World", 6, "Goodbye World", 8, 5); Console.WriteLine("String.Compare("Hello World", 6, " + ""Goodbye World", 8, 5) = " + result); } }