C# 先问一下,在CSDN如何获取积分,我找了半天还没找着,除了回答别人的问题获取积分外还有其他什么方式吗?买的点卡可以换积分的吗?刚入门的菜菜,有什么 水平去回答问题呢?还有积分高了有什么好处呢?是不是可以换money的呀?下边是我的问题:(因为内容多,手工是完成不了了。就想用这个程序来解决。。但是问题来了。。)
Document 是文档内容。
ss 是 数组:中国人*china
美国人*usa
小日本*japan
新加坡*singapo
泰国*tailand
然后我想把文档里的 内容全部用 * 后边的替换掉前边的内容。
但是在下边的遍历当中。只能是第一个元素在替换。也就是 word[0],手动改成word[1]或任何大于0的下标都报错:
未处理的异常:  System.IndexOutOfRangeException: 索引超出了数组界限。
            for (int i=0;i<ss.Length ;i++)
            {
                string[] word=ss[i].Split ('*');
                foreach (string lin in word)
                {
                    Console.WriteLine(lin);//这个地方它就只出来word[0]的结果: 中国人 china.怎么后边的就不出来呢??不是已经在for里边遍历了吗?
                }
             string result=   strdoc.Replace (word[0],word[1]);换种方式,用foreach遍历:
foreach (string scc in ss)
{
                document.Replace(scc.Substring(0, scc.IndexOf("*")), scc.Substring(scc.LastIndexOf("*" + 1)));
                Console.Write(document+'\n');
又出现这种错误:
未处理的异常:  System.ArgumentOutOfRangeException: StartIndex 不能小于 0。
参数名: startIndex
   在 System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length,
Boolean fAlwaysCopy)
   在 System.String.Substring(Int32 startIndex)等侯大虾

解决方案 »

  1.   


    string[] ss = { "中国人*china", "美国人*usa", "小日本*japan", "新加坡*singapo", "泰国*tailand" };
    string strdoc = "一个中国人一个美国人一个小日本一个新加坡一个泰国一个中国人一个美国人";
    foreach (string scc in ss)
    {
        string[] word = scc.Split('*');
        strdoc = strdoc.Replace(word[0], word[1]);
    }
    Console.Write(strdoc);
      

  2.   

    社区积分规则
    http://community.csdn.net/Help/Rule.htm
      

  3.   

    我测试过了问题解决了。谢谢雨纷飞啦
    还想请问一下为什么
    foreach   (string   scc   in   ss) 

                                    document.Replace(scc.Substring(0,   scc.IndexOf( "* ")),   scc.Substring(scc.LastIndexOf( "* "   +   1))); 
                                    Console.Write(document+ '\n '); 
    这个不行呢??我实在找不出错误的原因。现在 4:30. 明天结贴
      

  4.   

    LastIndexOf(   "*   "       +       1)
    改为LastIndexOf(   "*   " )      +       1
      

  5.   

    谢谢雨纷飞啦和hooo.hooo的方式也可以了。谢谢!!