Jump to content

How to break up strings from SQL?

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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...