//Microsoft Public License (Ms-PL)
//http://visualizer.codeplex.com/license
using System;
using System.Collections.Generic;
namespace Redwerb.BizArk.Core.StringExt
{
/// <summary>
/// Provides extension methods for strings.
/// </summary>
public static class StringExt
{
/// <summary>
/// Splits the string into lines.
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string[] Lines(this string str)
{
var lines = new List<string>();
using (var sr = new System.IO.StringReader(str))
{
string line = sr.ReadLine();
while (line != null)
{
lines.Add(line);
line = sr.ReadLine();
}
}
return lines.ToArray();
}
}
}
Author: coder
Forces the string to word wrap so that each line doesn't exceed the maxLineLength.
//Microsoft Public License (Ms-PL)
//http://visualizer.codeplex.com/license
using System;
using System.Collections.Generic;
namespace Redwerb.BizArk.Core.StringExt
{
/// <summary>
/// Provides extension methods for strings.
/// </summary>
public static class StringExt
{
/// <summary>
/// Forces the string to word wrap so that each line doesn't exceed the maxLineLength.
/// </summary>
/// <param name="str">The string to wrap.</param>
/// <param name="maxLength">The maximum number of characters per line.</param>
/// <returns></returns>
public static string Wrap(this string str, int maxLength)
{
return Wrap(str, maxLength, "");
}
/// <summary>
/// Forces the string to word wrap so that each line doesn't exceed the maxLineLength.
/// </summary>
/// <param name="str">The string to wrap.</param>
/// <param name="maxLength">The maximum number of characters per line.</param>
/// <param name="prefix">Adds this string to the beginning of each line.</param>
/// <returns></returns>
public static string Wrap(this string str, int maxLength, string prefix)
{
if (string.IsNullOrEmpty(str)) return "";
if (maxLength <= 0) return prefix + str;
var lines = new List<string>();
// breaking the string into lines makes it easier to process.
foreach (string line in str.Split("
".ToCharArray()))
{
var remainingLine = line.Trim();
do
{
var newLine = GetLine(remainingLine, maxLength - prefix.Length);
lines.Add(newLine);
remainingLine = remainingLine.Substring(newLine.Length).Trim();
// Keep iterating as int as we've got words remaining
// in the line.
} while (remainingLine.Length > 0);
}
return string.Join(Environment.NewLine + prefix, lines.ToArray());
}
private static string GetLine(string str, int maxLength)
{
// The string is less than the max length so just return it.
if (str.Length <= maxLength) return str;
// Search backwords in the string for a whitespace char
// starting with the char one after the maximum length
// (if the next char is a whitespace, the last word fits).
for (int i = maxLength; i >= 0; i--)
{
if (char.IsWhiteSpace(str[i]))
return str.Substring(0, i).TrimEnd();
}
// No whitespace chars, just break the word at the maxlength.
return str.Substring(0, maxLength);
}
}
}
Count lines in a string with IndexOf
public class MainClass{
public static long CountLinesInString(string text)
{
long count = 1;
int start = 0;
while ((start = text.IndexOf('
', start)) != -1)
{
count++;
start++;
}
return count;
}
}
Count number of chars in a string
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
public class MainClass{
public static int NumberOfCharsInStr(ref string stringToSearch, char charToFind)
{
int count = 0;
char[] chars = stringToSearch.ToCharArray();
foreach (char c in chars)
{
if (c == charToFind)
{
count++;
}
}
return count;
}
}
Calculate the max count of continuous characters
//GNU General Public License version 2 (GPLv2)
//http://cbasetest.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SDFL.Helper
{
public class StrHelper
{
///
/// For example, input “abb” and it will return 2, input “abbbcc” and it will return 3, etc..
///
///
/// [Dylan] 08/17/2009 Fix bug, if max list exist in the end, can't got it. e.g. “affddccccc”
/// [Dylan] 08/17/2009 Fix bug, if only one continuous list, and in the end, DupList is null, e.g. “aff”
/// [Dylan] 08/17/2009 Fix bug, if only one char. e.g “a”
///
///
///
public static Int32 MaxofDupCharacter(string str)
{
int length = str.Length;
bool isDup = false;
int iPre = 0;
int iLatter = 1;
List
// [Dylan] 08/17/2009 Fix bug, if only one char. e.g “a”
if (str.Length == 1)
return 1;
for (int i = 0; i < length -1; i++) { if (str[iLatter] == str[iPre]) { isDup = true; // [Dylan] 08/17/2009 Fix bug, if only one continuous list, and in the end, DupList is null, e.g. "aff" if (iLatter == length -1 && DupList.Count == 0) { DupList.Add(iLatter - iPre + 1); } // [Dylan] 08/17/2009 Fix bug, if max list exist in the end, can't got it. e.g. "affddccccc" // if true, it indicate that end region is still a continuous list. if (iPre< iLatter -1) { DupList.Add(iLatter - iPre+1); } } else { if (isDup) { DupList.Add(iLatter - iPre); } isDup = false; iPre = iLatter; } iLatter++; } return int.Parse(DupList.Max().ToString()); } } } [/csharp]
Get used chars
//The MIT License (MIT)
//http://arolibraries.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace AroLibraries.ExtensionMethods.Strings
{
public static class StringExt
{
public static IList
{
IList
for (int i = 0; i < iString.Length; i++)
{
char vChar = iString[i];
char cCharLower = Char.ToLower(vChar);
if (!vList.Contains(cCharLower) && Char.IsLetter(cCharLower))
{
vList.Add(cCharLower);
}
}
return vList;
}
}
}
[/csharp]
Reverse a string
//The MIT License (MIT)
//http://arolibraries.codeplex.com/license
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace AroLibraries.ExtensionMethods.Strings
{
public static class StringExt
{
public static string Ext_Reverse(this string s)
{
if (s == null) throw new ArgumentNullException("String is NULL");
char[] charArray = s.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
}
}