求大神关注一下这个 string.Substring() 出现奇怪异常的问题

寂小魔 2014-11-06 11:32:24
异常内容:
An unhandled exception of type 'System.StackOverflowException' occurred in System.Data.dll

[System.StackOverflowException]

{Cannot evaluate expression because the current thread is in a stack overflow state.}


//异常出现的代码
string str = " VillasBethlehemTauranga 3110";
string TempBeforStr = str.Substring(0, 23);//这里出现的异常
...全文
231 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
寂小魔 2014-11-06
  • 打赏
  • 举报
回复
引用 11 楼 Z65443344 的回复:
如果你想每23个字符放入数组 完全可以通过判断字符串的总长度,写个循环就搞定了 不要乱用递归
嗯,谢谢提醒。
於黾 2014-11-06
  • 打赏
  • 举报
回复
如果你想每23个字符放入数组 完全可以通过判断字符串的总长度,写个循环就搞定了 不要乱用递归
寂小魔 2014-11-06
  • 打赏
  • 举报
回复
果然是死循环了,造成了内存溢出造成的。
引用 1 楼 bdmh 的回复:
这两句没问题,是你其他的地方 有问题,是不是又死循环啥的,导致溢出错误
引用 3 楼 hrabeyond 的回复:
这两句代码是不是放在循环里的,有没有拼接字符串的行为
於黾 2014-11-06
  • 打赏
  • 举报
回复
如果是 string TempBeforStr = str.Substring(0, strLength); 报错 报错的时候, 看看str和strLength到底都是什么东西
xdashewan 2014-11-06
  • 打赏
  • 举报
回复
很可能无限递归了
於黾 2014-11-06
  • 打赏
  • 举报
回复
你这完整代码跟上面的代码一点都不一样好吗? 到底哪一行代码报错
寂小魔 2014-11-06
  • 打赏
  • 举报
回复
这个代码是在Show了一个窗体出来然后执行的,没有用线程 完整代码如下:

string str = “19 Brodie Place Unit 23Braemar VillasBethlehemTauranga 3110”;
string[] strEnter = str.Split('\n');
                for (int g = 0; g < strEnter.Length; g++)
                {
                    List<string> strList = CutSubstring(strEnter[g], 23, null);//换行
                }

/// <summary>
        /// 截取字符串长度分割
        /// </summary>
        /// <param name="str">判断需要分割字符串</param>
        /// <param name="strLength">设定分割长度</param>
        /// <param name="strList">分割长度字符串集合</param>
        /// <returns>分割长度字符串集合</returns>
        public static List<string> CutSubstring(string str,int strLength, List<string> strList)
        {
            if (strList == null)
                strList = new List<string>();
            if (str.Length > strLength)//判断字符串长度是否大于设定参数长度
            {//若大于
                string TempBeforStr = str.Substring(0, strLength);//取设定长度的字符串
                int LastLength = TempBeforStr.LastIndexOf(' ');//取此串字符中最后一个空格的索引
                if (LastLength < 0)//若不存在空格 则直接截取设定长度返回
                {
                    strList.Add(TempBeforStr);
                    return strList;
                }
                string BeforStr = str.Substring(0, LastLength);//获取未超过长度字符串
                strList.Add(BeforStr);
                string AfterStr = str.Substring(LastLength);//获取超过长度之后的字符串
                if (AfterStr.Length > strLength)//如果超过长度之后的字符串的长度仍大于设定长度,则再一次分割
                {
                    strList = CutSubstring(AfterStr, strLength, strList);
                }
                else
                {
                    strList.Add(AfterStr);
                }
            }
            else
            {//若小于,直接返回原字符串
                strList.Add(str);
                return strList;
            }
            return strList;
        }
引用 4 楼 xdashewan 的回复:
current thread is in a stack overflow state,你有用线程?貌似说线程堆栈溢出
引用 3 楼 hrabeyond 的回复:
这两句代码是不是放在循环里的,有没有拼接字符串的行为
引用 2 楼 GreyCandy 的回复:
这段代码是没问题的 你把整个代码贴出来看看
引用 1 楼 bdmh 的回复:
这两句没问题,是你其他的地方 有问题,是不是又死循环啥的,导致溢出错误
於黾 2014-11-06
  • 打赏
  • 举报
回复
你报错是System.Data.dll出现的异常,而substring跟这个dll一点关系都没有 还是找找异常到底出现在哪里吧 目测是你代码跟exe行号不一致,导致异常指向的行号错误了 清理下解决方案,重新生成 可以加个断点测试
xdashewan 2014-11-06
  • 打赏
  • 举报
回复
current thread is in a stack overflow state,你有用线程?貌似说线程堆栈溢出
手抓宝 2014-11-06
  • 打赏
  • 举报
回复
这两句代码是不是放在循环里的,有没有拼接字符串的行为
GreyCandy 2014-11-06
  • 打赏
  • 举报
回复
这段代码是没问题的 你把整个代码贴出来看看
bdmh 2014-11-06
  • 打赏
  • 举报
回复
这两句没问题,是你其他的地方 有问题,是不是又死循环啥的,导致溢出错误

110,533

社区成员

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

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

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