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

查看:112
本文介绍了如何分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们

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

代码:

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
	 }


这篇关于如何分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆