Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitting a string into 255 chars 1

Status
Not open for further replies.

jimberger

Programmer
Jul 5, 2001
222
0
0
GB
Hi,

I have a large string say maybe 1000 chars long and I want to split the string into 250 chars so i will have

str = 1000 chars long

do the split..

str1 = 250 chars long
str2 = 250 chars long
str3 = 250 chars long

etc..

does anyone know how to do this?

thanks for your time
 
hi,

no there isnt a delimeter unfortunately because i could then use the split function.

thanks
 
You could just get the length of the string and substring it in 250 character chunks. It wouldn't be that difficult of a piece of code to write, for any dynamic string length.

But, there is probably an easier way of doing it, I always seem to pick long and involved ways!

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Something like this maybe?
Code:
int i = 0;
int index = 0;
int len = srcString.Length;
if (len >= 250)
{
  while (i < len - 250)
  {
    destStrings[index] = srcString.Substring(i, 250);
    i += 250;
    index++;
  }
}
// pick up any remainder
if (len % 250 > 0)
{
   index++;
   destStrings[index] = srcString.Substring(i)
}
Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Yep, that's what I envisioned. But, when I get these grand schemes in my mind, someone always comes along with a regex or a built in function that would have done the work for me. Oh, well.

Star for your efforts, Chip.

----------------------------------------

TWljcm8kb2Z0J3MgIzEgRmFuIQ==
 
Yeah, the Split method wouldn't help in this case, as you don't have a delimiter to split on. So you have to do it the hard way.

Note that these are characters, not bytes.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Here is what I came up with but I do not like the way it handles the newline \n, or the tab \t. I used 5 just for testing.
Code:
using System; 
using System.Text.RegularExpressions; 
namespace regexsplitpattern  
{ 
	class MainClass 
	{	
		public static void Main(string[] args) 
		{ 
			string input = "12a\t3412b3412c3412d3412e341\n2";
			MatchCollection mc;
			Regex regex = new Regex("(.{5}|.+)",RegexOptions.Singleline); 
			mc = regex.Matches(input);
			for (int i = 0; i < mc.Count; i++) 
			{
				Console.WriteLine("value " + mc[i].Value);
			}
		}
	}
}
results
value 12a 3
value 412b3
value 412c3
value 412d3
value 412e3
value 41
2

Marty
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top