Word Wrap function for c# (.net)
Most of the technical posts I make on this blog, are issues that I struggle with for a day or two, and was unable to find a good solution for on the internet. Once I find the solution, I try to post it here, to try and make things easier on everyone else out there. I made a similar post to usenet microsoft.public.dotnet.faqs group last April, about doing word wrap in .net. This weekend, someone sent some feedback from my blog asking if that post was mine, and thanking me for the information. I really appreciated the comment, since it is hard to tell if people out there need and use the information I post.
In any case, I decided to repost the word wrap function to my blog, in hopes that more people can get some use out of it.
Code:
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;
}
.Net - String vs StringBuilder
Appending text a string object is done in this fashion:
stringObject += "more text";Concating strings like that is very fast and reliable when it comes to small C# strings. In fact it can be significantly simpler and faster than using the StringBuilder class.
The StringBuilder class on the other hand is excellent for processing large pieces of text. To add a string to a StringBuilder goes like this:
stringBuilderObject.Append("more text");The downside to the StringBuilder is at the end it needs to be converted back into a regular string. However if we are going to need to create some C# functions that will processes big amounts of text, then StringBuilder is the way to go. For more on the speed performace of string vs string builder visit the application optimization article.
In the C# String Processing Library functions are divided into two sections, the StringProcessing and the StringBuilderProcess classes, so you can run whichever one is more fitting.
Advanced Csharp String Functions
public static string Capitalize(string input)This C# function takes the first letter of a string an capitalizes it:
word -> Word
this is a sentence -> This is a sentence
public static bool IsCapitalized(string input)This C# function checks to see if the first letter of a string is capitalized:
Word -> True
word -> False
public static bool IsLowerCase(string input)Checks to see that an entire string is in lower cases
word -> True
Word -> False
public static bool IsUpperCase(string input)Checks to see that an entire string is in upper cases
Word -> False
WORD -> True
public static string SwapCases(string input)This C# function swaps the cases of a string
word -> WORD
Word -> wORD
public static string AlternateCases(string input)Takes the first character's casing an alternates the casing of the rest of the string
Hi -> Hi
helloworld -> hElLoWoRlD
public static string AlternateCases(string input, bool firstIsUpper) This C# string function works exactly the same except the user specifies on which case the string will start (Upper case or Lower case)
public static bool IsAlternateCases(string input)Checks to see whether a string has alternating cases
public static int CountTotal(string input, string chars, bool ignoreCases)Counts the total number of occurances of a string within another string
hello, l -> 2
hello, el -> 1
public static string RemoveVowels(string input)This C# string function removes the vowels in a string
hello -> hll
public static string KeepVowels(string input)This C# string function removes everything but the vowels in a string
hello -> eo
public static bool HasVowels(string input)Checks to see if there is any vowel present in a string
public static bool IsSpaces(string input)Quickly and effortlessly checks to see if a string is nothing but spaces
public static bool IsRepeatedChar(string input)Quickly and effortlessly checks to see if a string is nothing but the same letter repeated
aaaaaaaaaa -> True
aaaaaaaaad -> False
public static bool IsNumeric(string input)Processes a string to see if it contains only numbers
public static bool HasNumbers(string input)
Checks a string to see if it contains any numbers.
public static bool IsAlphaNumberic(string input)This C# function evaluates whether a string contains only numbers and letters (no symbols).
public static bool isLetters(string input)
Checks for a string to contain nothing but letters, no numbers or symbols.
public static string GetInitials(string input, bool capitalize, bool includeSpace)Converts a string, like a name, into its initials
Bob Landon -> B.L.
public static string GetTitle(string input)Capitalizes the first letter of every word in a string
the good story -> The Good Story
public static string GetNameCasing(string input)
(got it from google... =) )