Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have faced one proble,
actualy
i have a string of 1000 character...i have to cut it in to 150 character slot and put it into the table...
but one condition is spelling is not breaking while divide the string into 150 character slot....
Posted
Comments
Sergey Alexandrovich Kryukov 5-Mar-12 13:32pm    
First of all, it would be more useful to think on how come your ended up with such string in first place, instead of some structured information, and what would be the use of breaking it in pieces which have no structural meaning.

Why any arbitrary size of 150? Why not 153 or 256?

I would not be interested in giving and advice if the whole idea is doubtful...
--SA
Martin Arapovic 5-Mar-12 13:36pm    
Hi! What's your problem? Where you want to put your split-ed string? What Table? DataTable, Database table? Split string is pretty easy... Check my general solution used to split string...

Here is the one from many solutions to break string into peaces...
C#
public static class StringExtensions
{
	public static IEnumerable<string> SplitInGroups(this String str, int chunkSize)
	{
		if (null == str)
			throw new ArgumentNullException("str", "String can't be null.");
			
		if (chunkSize <= 0)
			throw new ArgumentException("Chunk size must be positive.", "chunkSize");
			
		for (var i = 0; i < str.Length; i += chunkSize)
			yield return str.Substring(i, Math.Min(chunkSize, str.Length - i));
	}
}
</string>


Then you can use this extension method on any string that you want to break...
C#
string yourString ="Some string...";

var stringGroups = yourString.SplitInGroups(150);
foreach(string stringGroup in stringGroups)
{
	// Do something with stringGroup...
}


Then you can iterate through enumerable of strings and do want you want with each group...
 
Share this answer
 
I assume you have a slot of max 150 chars. Then you're substring to put into the table, is the longest one that end with whitespace and is shorter then or equal to 150 chars long.


Piet
 
Share this answer
 

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