Click here to Skip to main content
14,299,038 members
Rate this:
Please Sign up or sign in to vote.
See more:
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.....
Posted
Updated 31-Jan-12 20:28pm
v2
Comments
   
Please remove the tag "ASP.NET" -- it is totally irrelevant, tag your language instead, that's all.
--SA
   
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
Jyothikarthik_N 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
Jyothikarthik_N 1-Feb-12 4:10am
   
Ok, Do check out the solution I've provided.
Kim Togo 1-Feb-12 2:14am
   
You want to split the string for every 40's chars?
SathyaRaju 1-Feb-12 2:45am
   
i wanna to split every 40 char...like this
4444444444444444
4444444444444444
44444444444444444
Tech Code Freak 1-Feb-12 2:58am
   
And maximum, how long can your string be?
Rate this:
Please Sign up or sign in to vote.

Solution 1

[EDIT]
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];

[EDIT]
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 2

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;
      }
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 3

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
	 }
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

Here's an even simpler solution :)

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 :)
   
v5
Comments
SathyaRaju 1-Feb-12 5:25am
   
i have one problem ...
if i give the space its not work.....format is changed
Jyothikarthik_N 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 .....
Jyothikarthik_N 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....
Tech Code Freak 1-Feb-12 5:45am
   
Why aren't you trying my solution?
If you have tried it, then give me some reply!
SathyaRaju 1-Feb-12 7:37am
   
thanks ...but string length is multiplied.....
Tech Code Freak 2-Feb-12 0:56am
   
Now I have edited it. Check my solution(Solution 1) again.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100