当前位置: 代码迷 >> ASP.NET >> StartIndex 不能小于 0。解决方法
  详细解决方案

StartIndex 不能小于 0。解决方法

热度:8352   发布时间:2013-02-25 00:00:00.0
StartIndex 不能小于 0。
1个返回Arraylist方法使用while筛选文本中的自定义标记比如aaaa[image]xxxxx[/image]bbbb
Arraylist先add("aaaa")再add("xxxxx")再重文本中Remove[image]xxxxx[/image]
以(先add 文本再add "xxxxx".)的格式
C# code
public static ArrayList getanythings(string body, string fs,string ends)    {        ArrayList imagepath=new ArrayList();//数组        int intf = body.IndexOf(fs);//前标索引        int inte = body.IndexOf(ends);//后标索引        bool inbody;//开关,段落是否包含1对标记        if (intf < inte) { inbody = true; }        else{inbody=false;}        while(inbody==true)//开        {        //返回[image]xxxxx[/image]        string temp = body.Substring(body.IndexOf(fs),body.IndexOf(ends)+ends.Length-body.IndexOf(fs));        //去掉[image][/image]返回xxxxx                string fpath = (temp.Substring(fs.Length, temp.Length - fs.Length - ends.Length));        //标记在文本首部先add " "        if (intf == 0)  { imagepath.Add(" "); body = body.Remove(body.IndexOf(fs),temp.Length); }             else  {                if(fpath.IndexOf(fs)==-1)   //判断标记内是否有嵌套的标记            {                         body= body.Remove(body.IndexOf(fs),temp.Length);  //有的话删除作废标记                        }        else              {           imagepath.Add(us.MyReplace(body.Substring(0, intf)));//添加文本到数组 us.MyReplace 是自己的处理string的方法            imagepath.Add(fpath);//添加标记内容xxxxx            body = body.Remove(body.IndexOf(fs), temp.Length);//删除已添加的标记             }                   }                       }        imagepath.Add(body);//添加最后条文本        return imagepath;//返回添加后最终数组    }

出错的地方:string temp = body.Substring(body.IndexOf(fs),body.IndexOf(ends)+ends.Length-body.IndexOf(fs));
StartIndex 不能小于 0。
可是我看temp 的输出正确!~~~帮我看看怎么回事!~





------解决方案--------------------------------------------------------
估计是减出负值了,你try catch一下看看,到底body.IndexOf(ends)+ends.Length-body.IndexOf(fs)值是多少
------解决方案--------------------------------------------------------
你把IndexOf得出-1时的body和fs的值粘出来,就知道了。
  相关解决方案