如何分割字符串[英] How to Split the string

问题描述

你好,朋友们

我有一个问题
我想把刺分开

代码:

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


它不工作请帮助我.....

推荐答案

[EDIT]
现在,在了解您的实际需求后,我开发了这个简短而甜蜜的代码:
将您的代码替换为:
string 地址 = 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)地址 += 地址[0].Substring(stInd);其他地址 += 地址[0].Substring(stInd, 40) + "<br/>";}}其他地址 = 地址[0];

[编辑]

使用这个功能
public string stringBreak(字符串 objstring, int intLength){string strChr = objstring;string objFinalString = "";if (strChr.Length > intLength){char[] sep = { ' '};字符串[] 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);}其他{试试{obj = strChrArray[objcount1].Substring(objcount, intLength);}catch(例外){obj = strChrArray[objcount1].Substring(objcount);}}objFinalString = objFinalString + "" + obj + " <br> ";objcount = objcount + intLength;}}其他{objFinalString = objFinalString + " " + strChrArray[objcount1];}objcount1 = objcount1 + 1;}strChr = objFinalString;}return strChr;}

嗨,

我可以看到,伙计们已经解决了你的问题,但这里有另一种解决方案......
试试这个简单的扩展方法...
public static class 扩展方法{public static IEnumerable<string>拆分(this string str, int chunkSize){return Enumerable.Range(0, str.Length/chunkSize).Select(i => str.Substring(i * chunkSize, chunkSize));}}</字符串>

用法:
string addressString = "12345678901234567890201234567890123456789040123456789012345678906012345678901234567890801234567890123456789020123456789012345678904012345678901234567890601234567890123456789080" ;var 地址 = addressString.Split(40);foreach(字符串地址in地址){//地址部分的事情}

本文地址:https://www.itbaoku.cn/post/1394939.html