AddaByte    Programmers' Script Repository
Free and low cost software.
spacer
Find out about our beautiful fractal backgrounds at abm-enterprises


Newsletter Signup
Name
Email
 

  C# - String handling - Wordwrap
 


Save up to 75% on software

Drive Clone

the best name in hosting


		public static string[] WordWrap(string text, int maxLength)
		{
			text = text.Replace("\n", " ");
			text = text.Replace("\r", " ");
			text = text.Replace(".", ". ");
			text = text.Replace(">", "> ");
			text = text.Replace("\t", " ");
			text = text.Replace(",", ", ");
			text = text.Replace(";", "; ");
			text = text.Replace("<br>", " ");
			text = text.Replace(" ", " ");

			string[] Words = text.Split(' ');
			int currentLineLength = 0;
			ArrayList Lines = new ArrayList(text.Length / maxLength);
			string currentLine = "";
			bool InTag = false;

			foreach (string currentWord in Words)
			{
				//ignore html
				if (currentWord.Length > 0)
				{

					if (currentWord.Substring(0,1) == "<")
						InTag = true;

					if (InTag)
					{
						//handle filenames inside html tags
						if (currentLine.EndsWith("."))
						{
							currentLine += currentWord;
						}
						else
							currentLine += " " + currentWord;

						if (currentWord.IndexOf(">") > -1)
							InTag = false;
					}
					else
					{
						if (currentLineLength + currentWord.Length + 1 < maxLength)
						{
							currentLine += " " + currentWord;
							currentLineLength += (currentWord.Length + 1);
						}
						else
						{
							Lines.Add(currentLine);
							currentLine = currentWord;
							currentLineLength = currentWord.Length;
						}
					}
				}

			}
			if (currentLine != "")
				Lines.Add(currentLine);

			string[] textLinesStr = new string[Lines.Count];
			Lines.CopyTo(textLinesStr, 0);
			return textLinesStr;


		}

		public static string[] Wrap(string text, int maxLength) 

		{ 
			text = text.Replace("\n", " "); 
			text = text.Replace("\r", " "); 
			text = text.Replace(".", ". "); 
			text = text.Replace(">", "> "); 
			text = text.Replace("\t", " "); 
			text = text.Replace(",", ", "); 
			text = text.Replace(";", "; "); 
			text = text.Replace(" ", " "); 
			text = text.Replace(" ", " "); 

			string[] Words = text.Split(' '); 
			int currentLineLength = 0; 
			ArrayList Lines = new ArrayList(text.Length / maxLength); 
			string currentLine = ""; 
			bool InTag = false; 

			foreach (string currentWord in Words) 
			{ 
				//ignore html 
				if (currentWord.Length > 0) 
				{ 
					if (currentWord.Substring(0,1) == "<") 
						InTag = true; 

					if (InTag) 
					{ 
						//handle filenames inside html tags 
						if (currentLine.EndsWith(".")) 
						{ 
							currentLine += currentWord; 

						} 
						else 
							currentLine += " " + currentWord; 
						if (currentWord.IndexOf(">") > -1) 
							InTag = false; 

					} 
					else 
					{ 
						if (currentLineLength + currentWord.Length + 1 < maxLength) 
						{ 
							currentLine += " " + currentWord; 
							currentLineLength += (currentWord.Length + 1); 
						} 
						else 
						{ 
							Lines.Add(currentLine); 
							currentLine = currentWord; 
							currentLineLength = currentWord.Length; 
						} 
					} 
				} 
			} 

			if (currentLine != "") 
				Lines.Add(currentLine); 
			string[] textLinesStr = new string[Lines.Count]; 
			Lines.CopyTo(textLinesStr, 0); 
			return textLinesStr; 

		}
  What does it do?
   no description given
 
  Features
  Added by Added Downloads Rating Popularity

  rpgivpgmr

1:39:24 AM Monday, October 19th 2009

4

not rated

 
 
  About the file
  Size Download Rate It  

  3894 KB
  download this file
 
 
  Comments
no comments
 
  Add your comment
 Email  
 Subject  
 Comment  
 Verification code:

 
    All comments are screened by the admin prior to being posted
     
 
  Contacts
  Report Abuse