Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hi, friends

I have one Problem
i wanna to split the sting

code:
C#
 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.....
Posted
Updated 31-Jan-12 20:28pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Feb-12 1:41am    
Please remove the tag "ASP.NET" -- it is totally irrelevant, tag your language instead, that's all.
--SA
Sergey Alexandrovich Kryukov 1-Feb-12 1:42am    
Not clear what is the expected result of splitting. And why this is a problem?
--SA
SathyaRaju 1-Feb-12 2:45am    
i wanna to split every 40 char...like this
4444444444444444
4444444444444444
44444444444444444
[no name] 1-Feb-12 1:52am    
Not clear. Please explain what you're trying to achieve!
SathyaRaju 1-Feb-12 2:45am    
i wanna to split every 40 char...like this
4444444444444444
4444444444444444
44444444444444444

Here's an even simpler solution :)

C#
public string stringBreak(string objstring, int intLength)
{
    return System.Text.RegularExpressions.Regex.Replace(objstring, "[\\d\\s]{" + intLength + "}", "$&<br>");
}


If this solves, please mark it as answer :)
 
Share this answer
 
v5
Comments
SathyaRaju 1-Feb-12 5:25am    
i have one problem ...
if i give the space its not work.....format is changed
[no name] 1-Feb-12 5:26am    
Space is given where? can you give an example?
SathyaRaju 1-Feb-12 5:35am    
string Address = "1234567890123 4567890201234567gdfghfg gggggggggggggggg gggggggggggggggggg gggggggggggggggggdfdf");

this type its not working .....
[no name] 1-Feb-12 5:40am    
I've updated the solution, that should solve I guess.
SathyaRaju 1-Feb-12 5:42am    
thanks....
me too try to solve the problem....
Hi,

I can see that guys are already solved your problem but here is another one solution...
Try this simple extension method...
C#
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:
C#
string addressString = "12345678901234567890201234567890123456789040123456789012345678906012345678901234567890801234567890123456789020123456789012345678904012345678901234567890601234567890123456789080";
	 
	 var addresses = addressString.Split(40);
	 
	 foreach(string address in addresses)
	 {
	 	// Dosomething with address part
	 }
 
Share this answer
 
Use this function
C#
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;
      }
 
Share this answer
 
Comments
SathyaRaju 1-Feb-12 3:00am    
ok thanks Anuja ...its work
Anuja Pawar Indore 1-Feb-12 3:01am    
You are welcome :)
SathyaRaju 1-Feb-12 5:09am    
i have one problem ...
if i give the space its not work.....format is changed
[EDIT]
Now, after knowing your actual requirement, I have developed this short and sweet code:
Replace your code with this:
C#
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];

[EDIT]
 
Share this answer
 
v6
Comments
nevin 2011 1-Feb-12 5:58am    
Great solution!

My 5!
SathyaRaju 2-Feb-12 1:14am    
thanks .....its works really super....
Amir Mahfoozi 12-Feb-12 2:16am    
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900