一个我也不懂意思的问题

huangsi007 2012-03-23 10:39:08
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;
}
}
...全文
109 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
再解释一下:
你的用法貌似是这样吧:

//去掉切割之后得到的为空字符串的数组元素
int c = china.Split(new string[]{"国"},StringSplitOptions.RemoveEmptyEntries).Length;
//切割之后得到的数组允许空字符串
int c1 = china.Split(new string[] { "国" }, StringSplitOptions.None).Length - 1;

[Quote=引用 4 楼 huangsi007 的回复:]
引用 2 楼 mngzilin 的回复:
int d=str.split(new string[]{"国"}).length;
if(str.startwith(“国”)&amp;&amp;str.endwith(“国”)) d=d+1;
if(收尾都不是国) d=d-1

能详细点吗 带注释
[/Quote]
  • 打赏
  • 举报
回复
最后一个是国字split一样会在国字后面取一个空字符串作为数组的最后一个元素[Quote=引用 2 楼 mngzilin 的回复:]
int d=str.split(new string[]{"国"}).length;
if(str.startwith(“国”)&&str.endwith(“国”)) d=d+1;
if(收尾都不是国) d=d-1
[/Quote]
  • 打赏
  • 举报
回复

//将国字替换为空,少几个就是几个国字
int count0 = china.Length - china.Replace("国","").Length;
//例如:3个国字可以讲字符串分成4份,那么长度-1就是国字数量
int count1 = china.Split('国').Length - 1;
huangsi007 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 mngzilin 的回复:]
int d=str.split(new string[]{"国"}).length;
if(str.startwith(“国”)&&str.endwith(“国”)) d=d+1;
if(收尾都不是国) d=d-1
[/Quote]
能详细点吗 带注释
  • 打赏
  • 举报
回复

//讲国字替换成空字符之后原来的字符串就会缩短,正好短了国字的数量的长度
int count0 = china.Length - china.Replace("国","").Length;
//以国字为分界点,比如1个国字可以分成2段,再减去1就是国字的数量
int count1 = china.Split('国').Length - 1;
mngzilin 2012-03-23
  • 打赏
  • 举报
回复
int d=str.split(new string[]{"国"}).length;
if(str.startwith(“国”)&&str.endwith(“国”)) d=d+1;
if(收尾都不是国) d=d-1
mngzilin 2012-03-23
  • 打赏
  • 举报
回复
int d=str.length-str.replace("国","").length

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧