Csharp/C Sharp/Data Types/String Compare
Содержание
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.rupare(str1, str2) == 0)
Console.WriteLine(str1 + " and " + str2 +
" are equal.");
else
Console.WriteLine(str1 + " and " + str2 +
" are not equal.");
if(String.rupare(str1, str3) == 0)
Console.WriteLine(str1 + " and " + str3 +
" are equal.");
else
Console.WriteLine(str1 + " and " + str3 +
" are not equal.");
if(String.rupare(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.rupare(str1, str5) == 0)
Console.WriteLine(str1 + " and " + str5 +
" are equal.");
else
Console.WriteLine(str1 + " and " + str5 +
" are not equal.");
if(String.rupare(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.rupare(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.rupare(head, 0, test, 0, head.Length, true));
tail = "Name";
test = "strVarName";
Console.WriteLine(string.rupare(tail, 0, test, (test.Length - tail.Length), tail.Length, true));
}
}
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(
"\nDoes 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();
}
}
}
Test for equality between stings.
using System;
using System.Text;
class StringApp {
static void Main(string[] args) {
string s1 = "Hello ";
string s2 = "World!";
Console.WriteLine("s1 == s2: {0}", s1 == s2);
Console.WriteLine();
}
}
use the Compare() method to compare strings
using System;
class MainClass {
public static void Main() {
int result;
result = String.rupare("bbc", "abc");
Console.WriteLine("String.rupare(\"bbc\", \"abc\") = " + result);
result = String.rupare("abc", "bbc");
Console.WriteLine("String.rupare(\"abc\", \"bbc\") = " + result);
result = String.rupare("bbc", "bbc");
Console.WriteLine("String.rupare(\"bbc\", \"bbc\") = " + result);
result = String.rupare("bbc", "BBC", true);
Console.WriteLine("String.rupare(\"bbc\", \"BBC\", true) = " + result);
result = String.rupare("bbc", "BBC", false);
Console.WriteLine("String.rupare(\"bbc\", \"BBC\", false) = " + result);
result = String.rupare("Hello World", 6, "Goodbye World", 8, 5);
Console.WriteLine("String.rupare(\"Hello World\", 6, " + "\"Goodbye World\", 8, 5) = " + result);
}
}
use the Equals() method and equality operator to check if two strings are equal
using System;
class MainClass {
public static void Main() {
string myString = String.Concat("Friends, ", "Romans");
string myString2 = String.Concat("Friends");
bool boolResult;
boolResult = String.Equals("bbc", "bbc");
Console.WriteLine("String.Equals(\"bbc\", \"bbc\") is " + boolResult);
boolResult = myString.Equals(myString2);
Console.WriteLine("myString.Equals(myString2) is " + boolResult);
boolResult = myString == myString2;
Console.WriteLine("myString == myString2 is " + boolResult);
}
}