15,959,738 members
See more:
Hi, friends

I have one Problem
i wanna to split the sting

code:
C#

int ee = Address[0].Length / 2;

its not working plz help me.....
Posted
Updated 31-Jan-12 20:28pm
v2
Sergey Alexandrovich Kryukov 1-Feb-12 1:41am
--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

## Solution 1

[EDIT]
Now, after knowing your actual requirement, I have developed this short and sweet code:
C#
int stInd = 0, cnt = Address[0].Length / 40;
{
for (int i = 0; i <= cnt; i++)
{
stInd = i * 40;
if (i == cnt)
else
}
}
else

[EDIT]

v6
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

## Solution 2

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;
}

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

## Solution 3

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#

{
}

## Solution 4

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>");
}

v5
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....