Jump to content

How to break up strings from SQL?


aic007

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...