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;
}
}
Month: February 2011
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);
}
}
}
Count Lines In 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 int Ext_CountLinesInString(this string str)
{
if (str == null) throw new ArgumentNullException("String is NULL");
int counter = 1;
string[] strTemp = str.Split(new string[] { "
" }, StringSplitOptions.None);
if (strTemp.Length > 0)
{
counter = strTemp.Length;
}
return counter;
}
}
}
Get Right Side Of 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_GetRightSideOfString(this string s, int count)
{
string newString = String.Empty;
if (s != null && count > 0)
{
int startIndex = s.Length - count;
if (startIndex > 0)
newString = s.Substring(startIndex, count);
else
newString = s;
}
return newString;
}
}
}

