如何分割字符串

编程入门 行业动态 更新时间:2024-10-24 19:22:01
本文介绍了如何分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

朋友们 我有一个问题 我想分开刺痛 代码:

Hi, friends I have one Problem i wanna to split the sting code:

Address[0] = "12345678901234567890201234567890123456789040123456789012345678906012345678901234567890801234567890123456789020123456789012345678904012345678901234567890601234567890123456789080"; string Addresses = string.Empty; int ee = Address[0].Length / 2; if (Address[0].Length > 40) Addresses = Address[0].Substring(0, Address[0].Length / 2) + "\n" + Address[0].Substring(Address[0].Length / 2, Address[0].Length / 2);

它不起作用请帮助我.....

its not working plz help me.....

推荐答案

现在,在了解了您的实际需求之后,我开发了这个简短而甜美的代码: 用以下代码替换您的代码: Now, after knowing your actual requirement, I have developed this short and sweet code: Replace your code with this: string Addresses = string.Empty; int stInd = 0, cnt = Address[0].Length / 40; if (Address[0].Length > 40) { for (int i = 0; i <= cnt; i++) { stInd = i * 40; if (i == cnt) Addresses += Address[0].Substring(stInd); else Addresses += Address[0].Substring(stInd, 40) + "<br/>"; } } else Addresses = Address[0];

使用此功能 Use this function public string stringBreak(string objstring, int intLength) { string strChr = objstring; string objFinalString = ""; if (strChr.Length > intLength) { char[] sep = { ' ' }; string[] strChrArray = strChr.Split(sep); int objcount1 = 0; while (objcount1 < strChrArray.Length) { if (strChrArray[objcount1].Length > intLength) { int i = 0; string obj = ""; int objcount = 0; objcount = 0; while (objcount < strChrArray[objcount1].Length) { if (objcount > strChrArray[objcount1].Length) { obj = strChrArray[objcount1].Substring(objcount - intLength); } else { try { obj = strChrArray[objcount1].Substring(objcount, intLength); } catch (Exception ex) { obj = strChrArray[objcount1].Substring(objcount); } } objFinalString = objFinalString + "" + obj + " <br> "; objcount = objcount + intLength; } } else { objFinalString = objFinalString + " " + strChrArray[objcount1]; } objcount1 = objcount1 + 1; } strChr = objFinalString; } return strChr; }

我可以看到伙计们已经解决了您的问题,但这是另一种解决方案... 试试这个简单的扩展方法... Hi, I can see that guys are already solved your problem but here is another one solution... Try this simple extension method... public static class ExtensionMethods { public static IEnumerable<string> Split(this string str, int chunkSize) { return Enumerable.Range(0, str.Length / chunkSize).Select(i => str.Substring(i * chunkSize, chunkSize)); } }</string>

用法:

Usage:

string addressString = "12345678901234567890201234567890123456789040123456789012345678906012345678901234567890801234567890123456789020123456789012345678904012345678901234567890601234567890123456789080"; var addresses = addressString.Split(40); foreach(string address in addresses) { // Dosomething with address part }

更多推荐

如何分割字符串

本文发布于:2023-11-29 00:30:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644574.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!