日期:2014-05-18  浏览次数:20695 次

System.Replace()和System.Split()方法怎么用啊
1.怎么用System.Replace()来实现下面的代码;
2.怎么用System.Split()来实现下面代码
注意:System.Replace()和System.Split()方法各举例一次!

using System;
using System.Collections.Generic;
using System.Text;

namespace P
{
  class Program
  {
  static void Main(string[] args)
  {
  string china = "中国一词的含义在不同时代也不同," +
  "大致统一时期略指全国,分裂时多指中原。" +
  "相传3000你年前,周公在阳城用土圭测度日影" +
  "测得夏至这一天午时,八尺之表周围景物均没有日影," +
  "便认为这是大地的中心,因此周朝谓之中国。";
  int count = Count(china, '国');
  Console .WriteLine("共有国字"+count +"个!");
  }
  public static int Count(string where,char find)
  {
  int total = 0;
  for (int i = 0; i < where.Length; i++)
  {
  if (where[i] == find)
  {
  total++;
  }
  }
  return total;
  }
  }
}


------解决方案--------------------
用Replace方法
C# code
            string china = "中国一词的含义在不同时代也不同," +
  "大致统一时期略指全国,分裂时多指中原。" +
  "相传3000你年前,周公在阳城用土圭测度日影" +
  "测得夏至这一天午时,八尺之表周围景物均没有日影," +
  "便认为这是大地的中心,因此周朝谓之中国。";
            int count = china.Length - china.Replace("国", "").Length;
            Console.WriteLine("共有国字" + count + "个!");
            Console.ReadKey();

------解决方案--------------------
Split:
C# code
            string china = "中国一词的含义在不同时代也不同," +
  "大致统一时期略指全国,分裂时多指中原。" +
  "相传3000你年前,周公在阳城用土圭测度日影" +
  "测得夏至这一天午时,八尺之表周围景物均没有日影," +
  "便认为这是大地的中心,因此周朝谓之中国。";
            int count = china.Split('国').Length - 1;
            Console.WriteLine("共有国字" + count + "个!");
            Console.ReadKey();