Write a C# Sharp program to determine whether a string ends with a particular substring

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to determine whether a string ends with a particular substring. 

Note : The results are affected by the choice of culture, whether case is ignored, and whether an ordinal comparison is performed.

Expected Output :

Determine whether a string ends with another string, using                       
different values of StringComparison.                                          
The current culture is en-US.                                                    
                                                                                 
StringComparison.CurrentCulture:                                                 
"xyzPQR" ends with "PQR".                                                        
"xyzPQR" ends with "PQR".

الأجوبة

using System;
using System.Threading;
class Example43 
{
    public static void Main() 
    {
    string str = "Determine whether a string ends with another string, " +
                   "using\n  different values of StringComparison.";
    StringComparison[] scValues = {
        StringComparison.CurrentCulture,
        StringComparison.CurrentCultureIgnoreCase,
        StringComparison.InvariantCulture,
        StringComparison.InvariantCultureIgnoreCase,
        StringComparison.Ordinal,
        StringComparison.OrdinalIgnoreCase };
//
    Console.Clear();
    Console.WriteLine(str);
// Display the current culture because the culture-specific comparisons
// can produce different results with different cultures.
    Console.WriteLine("The current culture is {0}.\n", 
                       Thread.CurrentThread.CurrentCulture.Name);
// Determine whether three versions of the letter I are equal to each other. 
    foreach (StringComparison sc in scValues)
        {
        Console.WriteLine("StringComparison.{0}:", sc);
        Test("xyzPQR", "PQR", sc);
        Test("xyzPQR", "PQR", sc);
        Console.WriteLine();
        }
    }
    protected static void Test(string x, string y, StringComparison comparison)
    {
    string resultFmt = ""{0}" {1} with "{2}".";
    string result = "does not end";
//
    if (x.EndsWith(y, comparison))
        result = "ends";
    Console.WriteLine(resultFmt, x, result, y);
    }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...