using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { object[] stuff = { "Hello", 20.9, 1, "There", "83", 99.99933 }; Console.WriteLine("The Stuff: {0} , {1} , {2} , {3} , {4} , {5} ", stuff); Console.WriteLine("{0}, Number{0}, Number{0}", 9); } }
Data Types
Use the static String.Format() method to build a new string.
using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { string formatStr; formatStr = String.Format("Don't you wish you had {0:C} in your account?", 99989.987); Console.WriteLine(formatStr); } }
use the Format() method to format a string
using System; class MainClass { public static void Main() { float myFloat = 1234.56789f; string myString8 = String.Format("{0, 10:f3}", myFloat); Console.WriteLine("String.Format("{0, 10:f3}", myFloat) = " + myString8); } }
Compare two strings with StringComparison.CurrentCultureIgnoreCase
using System; public static class StringExtensions { public static bool EqualsIgnoreCase(this string me, string them) { return me.Equals(them, StringComparison.CurrentCultureIgnoreCase); } }
Check if a String contain another string by ignoring case
using System; public static class StringExtensions { public static bool ContainsIgnoreCase(this string me, string other) { return me.ToLower().Contains(other.ToLower()); } }
Sample for String.Compare(String, Int32, String, Int32, Int32, Boolean), Honor 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; 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); } }
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); } }