2016年9月11日 星期日

[C#] char 與字串的分割 Split 方法

一、text.Split(分割字串, System.StringSplitOptions.RemoveEmptyEntries);
StringSplitOptions.RemoveEmptyEntries為排除空字串 
二、官網教學:


class TestStringSplit
{
  static void Main()
  {
  char[] delimiterChars = { ' ', ',', '.', ':', '\t' };

  string text = "one\ttwo three:four,five six seven";
  System.Console.WriteLine("Original text: '{0}'", text);

  string[] words = text.Split(delimiterChars);
  System.Console.WriteLine("{0} words in text:", words.Length);

  foreach (string s in words)
  {
  System.Console.WriteLine(s);
  }

  // Keep the console window open in debug mode.
  System.Console.WriteLine("Press any key to exit.");
  System.Console.ReadKey();
  }
}
/* Output:
  Original text: 'one two three:four,five six seven'
  7 words in text:
  one
  two
  three
  four
  five
  six
  seven
 */
三、官網教學:(官網貼太密了,所以把它貼出來)
class TestStringSplit
{
    static void Main()
    {
        char[] separatingChars = { "<<", "..." };
        string text = "one<<two......three<four";
        System.Console.WriteLine("Original text: '{0}'", text);
        string[] words = text.Split(separatingChars, System.StringSplitOptions.RemoveEmptyEntries);
        System.Console.WriteLine("{0} substrings in text:", words.Length);
        foreach (string s in words) { System.Console.WriteLine(s);
    } // Keep the console window open in debug mode. 
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();
    }
} /* Output: Original text: 'one<<two......three<four' 3 words in text: one two three<four */  
四、字串分割方法
String[] indexint;
String text1="1250*852";
方法一:indexint = text1.Split('*');
方法二:indexint= text1.Split(new string[] {"*" })



沒有留言:

張貼留言

影片的問題請留在影片的留言區裡。
部落格不會另外通知給我,所以很難發現你有留言。