Jump to content

How to break up strings from SQL?


aic007
 Share

Recommended Posts

Hi all.I am retreving data from SQL in a .NET C# application, and this works fine, but in some cases I get to much data that is unnecessary long and takes the whole width of the page displacing all other elements. So, I was wondering if anyone could tell me about a method which I can use to split up the string based on how many words I want the line to show before starting on a new line? This is how I get my data from SQL:addRow("History:", result.Rows[0]["HistorieFieldFromSQL"].ToString());Any tips for me? :-)

Link to comment
Share on other sites

give something like this a try. I tshould keep words whole.

static string ReplaceSpaceWithNewline(string s, int maxCharsInLine)		{			if (s.Length <= maxCharsInLine)				return s;			// search for last ' ' before maxCharsInLine			int breakPos = maxCharsInLine;			while ((breakPos > 0) && (s[breakPos] != ' '))				breakPos--;			// if none before, search for first ' ' after maxCharsInLine			if (breakPos == 0)			{				breakPos = maxCharsInLine + 1;				while ((breakPos < s.Length) && (s[breakPos] != ' '))					breakPos++;				// if none before or after, use entire string				if (breakPos == s.Length)					return s;			}			return s.Substring(0, breakPos) + '\n' +				ReplaceSpaceWithNewline(s.Substring(breakPos + 1), maxCharsInLine);		}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...