Csharp/C Sharp by API/System/String

Материал из .Net Framework эксперт
Версия от 12:12, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

new String(char[] charArray)

   
 
using System; 
 
class MainClass {  
  public static void Main() {  
 
    char[] charray = {"A", " ", "s", "t", "r", "i", "n", "g", "." }; 
    string str1 = new string(charray); 
    string str2 = "Another string."; 
 
    Console.WriteLine(str1); 
    Console.WriteLine(str2); 
  }  
}


String.Compare(String value1, String value2)

   
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);
  }
}


String.Compare(String value1, String value2, boolean case)

   
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);
  }
}


String.Concat(String value1, String value2)

   
using System;
class MainClass
{
  public static void Main()
  {
    string myString4 = String.Concat("A, ", "B");
    Console.WriteLine("String.Concat(\"A, \", \"B\") = "+ myString4);
    string myString5 = String.Concat("A, ", "B, ", "and countrymen");
    Console.WriteLine("String.Concat(\"A, \", \"B, \", " + "\"and countrymen\") = " + myString5);
  }
}


String.Contains

   
using System;  
 
class MainClass {  
  public static void Main() {  
    string str = "abcdefghijk"; 
 
    if(str.Contains("def"))  
      Console.WriteLine("The sequence "def" was found."); 
 
  }  
}


String.Copy(String value)

   
using System;
class MainClass
{
  public static void Main()
  {
    
    string myString4 = "string4";
    Console.WriteLine("myString4 = " + myString4);
    Console.WriteLine("Copying myString4 to myString7 using Copy()");
    string myString7 = String.Copy(myString4);
    Console.WriteLine("myString7 = " + myString7);
  }
}


String.Empty

   
using System;
using System.IO;
using System.Text;
public class MainClass
{
    public static void Main(string[] args)
    {
        string address = String.Empty;
    }
}


String.EndsWith()

  

using System;
class MainClass {
    public static void Main() {
        string[] myStrings = {"To", "be", "or", "not","to", "be"};
        string myString = String.Join(".", myStrings);        
        Console.WriteLine("myString = " + myString);
        if (myString.StartsWith("To")) {
            Console.WriteLine("myString starts with \"To\"");
        }
        if (myString.EndsWith("be")) {
            Console.WriteLine("myString ends with \"be\"");
        }
    
    }
}


String.Equals(String aString)

   
using System;
class MainClass
{
  public static void Main()
  {
    bool boolResult;
    string myString = "str";
    string myString2 = "str2";
    
    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);
  }
}


String.Format(String format, Float aFloat);

   
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);
  }
}


String.IndexOf

  

using System; 
 
public class StringSearchDemo { 
  public static void Main() { 
    string str = "C# has powerful string handling."; 
    int idx; 
 
    Console.WriteLine("str: " + str); 
 
    idx = str.IndexOf("h"); 
    Console.WriteLine("Index of first "h": " + idx); 
 
    idx = str.LastIndexOf("h"); 
    Console.WriteLine("Index of last "h": " + idx); 
 
    idx = str.IndexOf("ing"); 
    Console.WriteLine("Index of first \"ing\": " + idx); 
 
    idx = str.LastIndexOf("ing"); 
    Console.WriteLine("Index of last \"ing\": " + idx); 
 
    char[] chrs = { "a", "b", "c" }; 
    idx = str.IndexOfAny(chrs); 
    Console.WriteLine("Index of first "a", "b", or "c": " + idx); 
 
    if(str.StartsWith("C# has")) 
      Console.WriteLine("str begins with \"C# has\""); 
 
    if(str.EndsWith("ling.")) 
      Console.WriteLine("str ends with \"ling.\""); 
  } 
}


String.IndexOfAny(char[] myChars)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    char[] myChars = {"b", "e"};
    int index = myString.IndexOfAny(myChars);
    Console.WriteLine(""b" and "e" occur at index " + index + " of myString");
    index = myString.LastIndexOfAny(myChars);
    Console.WriteLine(""b" and "e" last occur at index " + index + " of myString");
  }
}


String.Insert(int index, String value)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    string myString10 = myString.Insert(6, "A, ");
    Console.WriteLine("myString.Insert(6, \"A, \") = " + myString10);
    string myString11 = myString10.Remove(14, 7);
    Console.WriteLine("myString10.Remove(14, 7) = " + myString11);
    string myString12 = myString11.Replace(",", "?");
    Console.WriteLine("myString11.Replace(",", "?") = " + myString12);
    string myString13 = myString12.Replace("to be", "Or not to be A");
    Console.WriteLine("myString12.Replace(\"to be\", \"Or not to be A\") = " + myString13);
  }
}


String.Join(String join, String value)

   
using System;
class MainClass
{
  public static void Main()
  {
    
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString9 = String.Join(".", myStrings);
    Console.WriteLine("myString9 = " + myString9);
  }
}


String.LastIndexOf

  
using System; 
 
public class StringSearchDemo { 
  public static void Main() { 
    string str = "C# has powerful string handling."; 
    int idx; 
 
    Console.WriteLine("str: " + str); 
 
    idx = str.IndexOf("h"); 
    Console.WriteLine("Index of first "h": " + idx); 
 
    idx = str.LastIndexOf("h"); 
    Console.WriteLine("Index of last "h": " + idx); 
 
    idx = str.IndexOf("ing"); 
    Console.WriteLine("Index of first \"ing\": " + idx); 
 
    idx = str.LastIndexOf("ing"); 
    Console.WriteLine("Index of last \"ing\": " + idx); 
 
    char[] chrs = { "a", "b", "c" }; 
    idx = str.IndexOfAny(chrs); 
    Console.WriteLine("Index of first "a", "b", or "c": " + idx); 
 
    if(str.StartsWith("C# has")) 
      Console.WriteLine("str begins with \"C# has\""); 
 
    if(str.EndsWith("ling.")) 
      Console.WriteLine("str ends with \"ling.\""); 
  } 
}


String.LastIndexOfAny(char[] myChars)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    char[] myChars = {"b", "e"};
    int index = myString.IndexOfAny(myChars);
    Console.WriteLine(""b" and "e" occur at index " + index + " of myString");
    index = myString.LastIndexOfAny(myChars);
    Console.WriteLine(""b" and "e" last occur at index " + index + " of myString");
  }
}


String.Length

   
using System;  
  
class MainClass {   
  public static void Main() {   
    string str1 = "ABCDEabcde1234567890";   
    
    Console.WriteLine("Display str1, one char at a time."); 
    for(int i=0; i < str1.Length; i++)  
      Console.Write(str1[i]);   
    Console.WriteLine("\n");   
  }
}


String.PadLeft(int len)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    string myString14 = "(" + myString.PadLeft(20) + ")";
    Console.WriteLine(""(" + myString.PadLeft(20) + ")" = " + myString14);
    string myString15 = "(" + myString.PadLeft(20, ".") + ")";
    Console.WriteLine(""(" + myString.PadLeft(20, ".") = " + myString15);
    string myString16 = "(" + myString.PadRight(20) + ")";
    Console.WriteLine(""(" + myString.PadRight(20) + ")" = " + myString16);
    string myString17 = "(" + myString.PadRight(20, ".") + ")";
    Console.WriteLine(""(" + myString.PadRight(20, ".") + ")" = " + myString17);
  }
}


String.PadLeft(int len, char ch)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    string myString14 = "(" + myString.PadLeft(20) + ")";
    Console.WriteLine(""(" + myString.PadLeft(20) + ")" = " + myString14);
    string myString15 = "(" + myString.PadLeft(20, ".") + ")";
    Console.WriteLine(""(" + myString.PadLeft(20, ".") = " + myString15);
    string myString16 = "(" + myString.PadRight(20) + ")";
    Console.WriteLine(""(" + myString.PadRight(20) + ")" = " + myString16);
    string myString17 = "(" + myString.PadRight(20, ".") + ")";
    Console.WriteLine(""(" + myString.PadRight(20, ".") + ")" = " + myString17);
  }
}


String.PadRight(int len)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    string myString14 = "(" + myString.PadLeft(20) + ")";
    Console.WriteLine(""(" + myString.PadLeft(20) + ")" = " + myString14);
    string myString15 = "(" + myString.PadLeft(20, ".") + ")";
    Console.WriteLine(""(" + myString.PadLeft(20, ".") = " + myString15);
    string myString16 = "(" + myString.PadRight(20) + ")";
    Console.WriteLine(""(" + myString.PadRight(20) + ")" = " + myString16);
    string myString17 = "(" + myString.PadRight(20, ".") + ")";
    Console.WriteLine(""(" + myString.PadRight(20, ".") + ")" = " + myString17);
  }
}


String.PadRight(int len, char ch)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    string myString14 = "(" + myString.PadLeft(20) + ")";
    Console.WriteLine(""(" + myString.PadLeft(20) + ")" = " + myString14);
    string myString15 = "(" + myString.PadLeft(20, ".") + ")";
    Console.WriteLine(""(" + myString.PadLeft(20, ".") = " + myString15);
    string myString16 = "(" + myString.PadRight(20) + ")";
    Console.WriteLine(""(" + myString.PadRight(20) + ")" = " + myString16);
    string myString17 = "(" + myString.PadRight(20, ".") + ")";
    Console.WriteLine(""(" + myString.PadRight(20, ".") + ")" = " + myString17);
  }
}


String.Remove(int start, int end)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    string myString10 = myString.Insert(6, "A, ");
    Console.WriteLine("myString.Insert(6, \"A, \") = " + myString10);
    string myString11 = myString10.Remove(14, 7);
    Console.WriteLine("myString10.Remove(14, 7) = " + myString11);
    string myString12 = myString11.Replace(",", "?");
    Console.WriteLine("myString11.Replace(",", "?") = " + myString12);
    string myString13 = myString12.Replace("to be", "Or not to be A");
    Console.WriteLine("myString12.Replace(\"to be\", \"Or not to be A\") = " + myString13);
  }
}


String.Replace(char ch, char ch2)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    string myString10 = myString.Insert(6, "A, ");
    Console.WriteLine("myString.Insert(6, \"A, \") = " + myString10);
    string myString11 = myString10.Remove(14, 7);
    Console.WriteLine("myString10.Remove(14, 7) = " + myString11);
    string myString12 = myString11.Replace(",", "?");
    Console.WriteLine("myString11.Replace(",", "?") = " + myString12);
    string myString13 = myString12.Replace("to be", "Or not to be A");
    Console.WriteLine("myString12.Replace(\"to be\", \"Or not to be A\") = " + myString13);
  }
}


String.Replace(String value1, String value2)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    string myString10 = myString.Insert(6, "A, ");
    Console.WriteLine("myString.Insert(6, \"A, \") = " + myString10);
    string myString11 = myString10.Remove(14, 7);
    Console.WriteLine("myString10.Remove(14, 7) = " + myString11);
    string myString12 = myString11.Replace(",", "?");
    Console.WriteLine("myString11.Replace(",", "?") = " + myString12);
    string myString13 = myString12.Replace("to be", "Or not to be A");
    Console.WriteLine("myString12.Replace(\"to be\", \"Or not to be A\") = " + myString13);
  }
}


String.Split(String splittor)

   
using System;
class MainClass
{
  public static void Main()
  {
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString9 = String.Join(".", myStrings);    
    myStrings = myString9.Split(".");
    foreach (string mySplitString in myStrings)
    {
      Console.WriteLine("mySplitString = " + mySplitString);
    }
  }
}


String.StartsWith()

  
using System;
class MainClass {
    public static void Main() {
        string[] myStrings = {"To", "be", "or", "not","to", "be"};
        string myString = String.Join(".", myStrings);        
        Console.WriteLine("myString = " + myString);
        if (myString.StartsWith("To")) {
            Console.WriteLine("myString starts with \"To\"");
        }
        if (myString.EndsWith("be")) {
            Console.WriteLine("myString ends with \"be\"");
        }
    
    }
}


String == String

   
using System;
class MainClass
{
  public static void Main()
  {
    bool boolResult;
    string myString = "str";
    string myString2 = "str2";
    
    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);
  }
}


String.SubString(int pos)

   
using System;
class MainClass
{
  public static void Main()
  {
    
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    
    string myString21 = myString.Substring(3);
    Console.WriteLine("myString.Substring(3) = " + myString21);
    string myString22 = myString.Substring(3, 2);
    Console.WriteLine("myString.Substring(3, 2) = " + myString22);
  }
}


String.SubString(int start,)

   
using System;
class MainClass
{
  public static void Main()
  {
    
    string[] myStrings = {"To", "be", "or", "not", "to", "be"};
    string myString = String.Join(".", myStrings);   
    
    
    string myString21 = myString.Substring(3);
    Console.WriteLine("myString.Substring(3) = " + myString21);
    string myString22 = myString.Substring(3, 2);
    Console.WriteLine("myString.Substring(3, 2) = " + myString22);
  }
}


String.ToCharArray

   
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
public class MainClass
{
    public static void Main()
    {
        string s = "Hello, World.";
        char[] c1 = s.ToCharArray();
        char[] c2 = s.ToCharArray(0, 5);
    }
}


String.ToCharArray(int start,)

   
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
public class MainClass
{
    public static void Main()
    {
        string s = "Hello, World.";
        char[] c1 = s.ToCharArray();
        char[] c2 = s.ToCharArray(0, 5);
    }
}


String.ToLower()

  
using System;  
  
class MainClass {   
  public static void Main() {   
    string str1 = "ABCDEabcde1234567890";   
    
    string strLow = str1.ToLower(); 
    string strUp =  str1.ToUpper(); 
    Console.WriteLine("Lowercase version of str1:\n " +  strLow); 
    Console.WriteLine("Uppercase version of str1:\n " +  strUp); 
  }
}


String.ToUpper()

  
using System;  
  
class MainClass {   
  public static void Main() {   
    string str1 = "ABCDEabcde1234567890";   
    
    string strLow = str1.ToLower(); 
    string strUp =  str1.ToUpper(); 
    Console.WriteLine("Lowercase version of str1:\n " +  strLow); 
    Console.WriteLine("Uppercase version of str1:\n " +  strUp); 
  }
}


String.Trim()

   
using System;
class MainClass
{
  public static void Main()
  {
    
    string myString18 = "(" + "  Whitespace  ".Trim() + ")";
    Console.WriteLine(""(" + \"  Whitespace  \".Trim() + ")" = " + myString18);
    string myString19 = "(" + "  Whitespace  ".TrimStart() + ")";
    Console.WriteLine(""(" + \"  Whitespace  \".TrimStart() + ")" = " + myString19);
    string myString20 = "(" + "  Whitespace  ".TrimEnd() + ")";
    Console.WriteLine(""(" + \"  Whitespace  \".TrimEnd() + ")" = " + myString20);
  }
}


String.TrimEnd()

   
using System;
class MainClass
{
  public static void Main()
  {
    
    string myString18 = "(" + "  Whitespace  ".Trim() + ")";
    Console.WriteLine(""(" + \"  Whitespace  \".Trim() + ")" = " + myString18);
    string myString19 = "(" + "  Whitespace  ".TrimStart() + ")";
    Console.WriteLine(""(" + \"  Whitespace  \".TrimStart() + ")" = " + myString19);
    string myString20 = "(" + "  Whitespace  ".TrimEnd() + ")";
    Console.WriteLine(""(" + \"  Whitespace  \".TrimEnd() + ")" = " + myString20);
  }
}


String.TrimStart()

   
using System;
class MainClass
{
  public static void Main()
  {
    
    string myString18 = "(" + "  Whitespace  ".Trim() + ")";
    Console.WriteLine(""(" + \"  Whitespace  \".Trim() + ")" = " + myString18);
    string myString19 = "(" + "  Whitespace  ".TrimStart() + ")";
    Console.WriteLine(""(" + \"  Whitespace  \".TrimStart() + ")" = " + myString19);
    string myString20 = "(" + "  Whitespace  ".TrimEnd() + ")";
    Console.WriteLine(""(" + \"  Whitespace  \".TrimEnd() + ")" = " + myString20);
  }
}