Thursday, July 15, 2010

String Functions in C#

Swaps the cases in a string
//Suresh -> sURESH, SURESH -> suresh, SuReSh -> sUrEsH
public string SwapCases(string sInput)
{
string sRetValue = "";
for (int i = 0; i < sInput.Length; i++)
{
if (string.Compare(sInput.Substring(i, 1), sInput.Substring(i, 1).ToUpper(), false) == 0)
sRetValue += sInput.Substring(i, 1).ToLower();
else
sRetValue += sInput.Substring(i, 1).ToUpper();
}
return sRetValue;
}


Alternates cases between letters of a string, letting the user pick if the first letter is capitalized
public string AlternateCases(string sInput, bool startWithUpper)
{
string sRetValue = "";
for (int i = 0; i < sInput.Length; i++)
{
if (startWithUpper)
sRetValue += sInput.Substring(i, 1).ToUpper();
else
sRetValue += sInput.Substring(i, 1).ToLower();
startWithUpper = !startWithUpper;
}
return sRetValue;
}

Removes vowels from a word
//Suresh-> Srsh
public string RemoveVowels(string sInput)
{
string sRetValue = "";
string Letter;
for (int i = 0; i < sInput.Length; i++)
{
Letter = sInput.Substring(i, 1);
if (string.Compare(Letter, "a", true) != 0 && string.Compare(Letter, "e", true) != 0 && string.Compare(Letter, "i", true) != 0 && string.Compare(Letter, "o", true) != 0 && string.Compare(Letter, "u", true) != 0)
{
sRetValue += Letter;
}
}
return sRetValue;
}

Keep vowels from a word
//Suresh -> ue
public string KeepVowels(string sInput)
{
string sRetValue = "";
string Letter;
for (int i = 0; i < sInput.Length; i++)
{
Letter = sInput.Substring(i, 1);
if (string.Compare(Letter, "a", true) == 0 || string.Compare(Letter, "e", true) == 0 || string.Compare(Letter, "i", true) == 0 || string.Compare(Letter, "o", true) == 0 || string.Compare(Letter, "u", true) == 0)
{
sRetValue += Letter;
}
}
return sRetValue;
}

Returns an array converted into a string
public string ArrayToString(Array sInput, string separator)
{
string sRetValue = "";
for (int i = 0; i < sInput.Length; i++)
{
sRetValue += sInput.GetValue(i).ToString();
if (i != sInput.Length - 1)
sRetValue += separator;
}
return sRetValue;
}

Inserts a separator after every letter
//Suresh,- -> S-u-r-e-s-h
public string InsertSeparator(string sInput, string separator)
{
string sRetValue = "";
for (int i = 0; i < sInput.Length; i++)
{
sRetValue += sInput.Substring(i, 1);
if (i != sInput.Length - 1)
sRetValue += separator;
}
return sRetValue;
}

Inserts a separator after every Count letters
//Suresh, -, 2 -> Su-re-sh
public string InsertSeparatorEvery(string sInput, string separator, int count)
{
string sRetValue = "";
for (int i = 0; i < sInput.Length; i+=count)
{
if (i + count < sInput.Length)
sRetValue += sInput.Substring(i, count);
else
sRetValue += sInput.Substring(i);
if (i < sInput.Length - count)
sRetValue += separator;
}
return sRetValue;
}

Reverses a string
//Suresh -> hseruS
public string Reverse(string sInput)
{
string sRetValue = "";
for (int i = sInput.Length - 1; i >= 0; i--)
{
sRetValue += sInput.Substring(i, 1);
}
return sRetValue;
}



Capitalizes a word or sentence
//suresh -> Suresh, my self suresh jangid -> My Self Suresh Jangid
public string Capitalize(string sInput)
{
if (sInput.Length == 0) return "";
if (sInput.Length == 1) return sInput.ToUpper();
return sInput.Substring(0, 1).ToUpper() + sInput.Substring(1);
}

Checks whether a word or sentence is capitalized
//Suresh -> True, My self suresh jangid -> True
public bool IsCapitalized(string sInput)
{
if (sInput.Length == 0) return false;
return string.Compare(sInput.Substring(0, 1), sInput.Substring(0, 1).ToUpper(), false) == 0;
}

Checks whether a string is in all lower case
//suresh -> True, Suresh -> False
public bool IsLowerCase(string sInput)
{
for (int i = 0; i < sInput.Length; i++)
{
if (string.Compare(sInput.Substring(i, 1), sInput.Substring(i, 1).ToLower(), false) != 0)
return false;
}
return true;
}

Checks whether a string is in all upper case
//suresh -> False, Suresh -> False, SURESH -> True
public bool IsUpperCase(string sInput)
{
for (int i = 0; i < sInput.Length; i++)
{
if (string.Compare(sInput.Substring(i, 1), sInput.Substring(i, 1).ToUpper(), false) != 0)
return false;
}
return true;
}

Alternates cases between letters of a string, first letter's case stays the same
//Bi -> Bi, suresh -> SuReSh
public string AlternateCases(string sInput)
{
if (sInput.Length == 0) return "";
if (sInput.Length == 1) return sInput; //Cannot automatically alternate
bool firstIsUpper = string.Compare(sInput.Substring(0, 1), sInput.Substring(0, 1).ToUpper(), false) != 0;
string sRetValue = sInput.Substring(0, 1);
for (int i = 1; i < sInput.Length; i++)
{
if (firstIsUpper)
sRetValue += sInput.Substring(i, 1).ToUpper();
else
sRetValue += sInput.Substring(i, 1).ToLower();
firstIsUpper = !firstIsUpper;
}
return sRetValue;
}

Checks to see if a string has alternate cases
//sUrEsH -> True
public bool IsAlternateCases(string sInput)
{
if (sInput.Length <= 1) return false;

bool lastIsUpper = string.Compare(sInput.Substring(0, 1), sInput.Substring(0, 1).ToUpper(), false) == 0;

for (int i = 1; i < sInput.Length; i++)
{
if (lastIsUpper)
{
if (string.Compare(sInput.Substring(i, 1), sInput.Substring(i, 1).ToLower(), false) != 0)
return false;
}
else
{
if (string.Compare(sInput.Substring(i, 1), sInput.Substring(i, 1).ToUpper(), false) != 0)
return false;
}

lastIsUpper = !lastIsUpper;
}

return true;
}

Counts total number of a char or chars in a string
//Suresh, s -> 2, Suresh, re -> 1
public int CountTotal(string sInput, string chars, bool ignoreCases)
{
int count = 0;
for (int i = 0; i < sInput.Length; i++)
{
if (!(i + chars.Length > sInput.Length) && string.Compare(sInput.Substring(i, chars.Length), chars, ignoreCases) == 0)
{
count++;
}
}
return count;
}

Checks to see if a string contains vowels
//Suresh -> True, Srsh -> False
public bool HasVowels(string sInput)
{
string currentLetter;
for (int i = 0; i < sInput.Length; i++)
{
currentLetter = sInput.Substring(i, 1);

if (string.Compare(currentLetter, "a", true) == 0 ||
string.Compare(currentLetter, "e", true) == 0 ||
string.Compare(currentLetter, "i", true) == 0 ||
string.Compare(currentLetter, "o", true) == 0 ||
string.Compare(currentLetter, "u", true) == 0)
{
//A vowel found
return true;
}
}

return false;
}

Checks if string is nothing but spaces
//" " -> True
public bool IsSpaces(string sInput)
{
if (sInput.Length == 0) return false;
return sInput.Replace(" ", "").Length == 0;
}

Checks if the string has all the same letter/number
//aaaaaa -> true, aaaaaaaab -> false
public bool IsRepeatedChar(string sInput)
{
if (sInput.Length == 0) return false;
return sInput.Replace(sInput.Substring(0, 1), "").Length == 0;
}

Checks if string has only numbers
//12453 -> True, 234d3 -> False
public bool IsNumeric(string sInput)
{
for (int i = 0; i < sInput.Length; i++)
{
if (!(Convert.ToInt32(sInput[i]) >= 48 && Convert.ToInt32(sInput[i]) <= 57))
{
//Not integer value
return false;
}
}
return true;
}

Checks if the string contains numbers
//suresh -> False, sure65sh -> True
public bool HasNumbers(string sInput)
{
return System.Text.RegularExpressions.Regex.IsMatch(sInput, "\\d+");
}

Checks if string is numbers and letters
//suresh34 -> True, $chool! -> False
public bool IsAlphaNumberic(string sInput)
{
char currentLetter;
for (int i = 0; i < sInput.Length; i++)
{
currentLetter = sInput[i];

if (!(Convert.ToInt32(currentLetter) >= 48 && Convert.ToInt32(currentLetter) <= 57) &&
!(Convert.ToInt32(currentLetter) >= 65 && Convert.ToInt32(currentLetter) <= 90) &&
!(Convert.ToInt32(currentLetter) >= 97 && Convert.ToInt32(currentLetter) <= 122))
{
//Not a number or a letter
return false;
}
}
return true;
}

Checks if a string contains only letters
//Hi -> True, Hi123 -> False
public bool isLetters(string sInput)
{
char currentLetter;
for (int i = 0; i < sInput.Length; i++)
{
currentLetter = sInput[i];

if (!(Convert.ToInt32(currentLetter) >= 65 && Convert.ToInt32(currentLetter) <= 90) &&
!(Convert.ToInt32(currentLetter) >= 97 && Convert.ToInt32(currentLetter) <= 122))
{
//Not a letter
return false;
}
}
return true;
}

Returns the initials of a name or sentence
//capitalize - whether to make intials capitals
//includeSpace - to return intials separated (True - S. J. or False - S.J.)
//Suresh Jangid -> S. J. or S.J.
public string GetInitials(string sInput, bool capitalize, bool includeSpace)
{
string[] words = sInput.Split(new char[] { ' ' });

for (int i = 0; i < words.Length; i++)
{
if (words[i].Length > 0)
if (capitalize)
words[i] = words[i].Substring(0, 1).ToUpper() + ".";
else
words[i] = words[i].Substring(0, 1) + ".";
}

if (includeSpace)
return string.Join(" ", words);
else
return string.Join("", words);
}

Capitalizes the first letter of every word
//the earth -> The Earth
public string GetTitle(string sInput)
{
string[] words = sInput.Split(new char[] { ' ' });

for (int i = 0; i < words.Length; i++)
{
if (words[i].Length > 0)
words[i] = words[i].Substring(0, 1).ToUpper() + words[i].Substring(1);
}

return string.Join(" ", words);
}


Checks whether the first letter of each word is capitalized
//The Earth -> True, the earth -> False
public bool IsTitle(string sInput)
{
string[] words = sInput.Split(new char[] { ' ' });

for (int i = 0; i < words.Length; i++)
{
if (words[i].Length > 0)
if (string.Compare(words[i].Substring(0, 1).ToUpper(), words[i].Substring(0, 1), false) != 0)
return false;
}
return true;
}

Returns all the locations of a char in a string
//SURESH, s -> 0, 4
public int[] IndexOfAll(string sInput, string chars)
{
List indices = new List();
for (int i = 0; i < sInput.Length; i++)
{
if (sInput.Substring(i, 1) == chars)
indices.Add(i);
}

if (indices.Count == 0)
indices.Add(-1);

return indices.ToArray();
}


Gets the char in a string at a given position, but counting from right to left
//Suresh, 2 -> e
public char CharRight(string sInput, int index)
{
if (sInput.Length - index - 1 >= sInput.Length ||
sInput.Length - index - 1 < 0)
return new char();

string str = sInput.Substring(sInput.Length - index - 1, 1);
return str[0];
}

Gets the char in a string from a starting position plus the index
//suresh, 3, 1 -> s
public char CharMid(string sInput, int startingIndex, int countIndex)
{
if (startingIndex + countIndex < sInput.Length)
{
string str = sInput.Substring(startingIndex + countIndex, 1);
return str[0];
}
else
return new char();
}

Function that works the same way as the default Substring, but it takes Start and End (exclusive) parameters instead of Start and Length
//Suresh, 1, 3 -> ur
public string SubstringEnd(string sInput, int start, int end)
{
if (start > end) //Flip the values
{
start ^= end;
end = start ^ end;
start ^= end;
}

if (end > sInput.Length) end = sInput.Length; //avoid errors

return sInput.Substring(start, end - start);

}

Splits strings, but leaves anything within quotes
//(Has issues with nested quotes
//This is a "Suresh jangid" ->
//This
//is
//a
//Suresh Jangid
public string[] SplitQuotes(string sInput, bool ignoreQuotes, string separator)
{
if (ignoreQuotes)
return sInput.Split(separator.ToCharArray());
else
{
string[] words = sInput.Split(separator.ToCharArray());
List newWords = new List();

for (int i = 0; i < words.Length; i++)
{
if (words[i].StartsWith('"'.ToString()))
{
List linked = new List();
for (int y = i; y < words.Length; y++)
{
if (words[y].EndsWith('"'.ToString()))
{
linked.Add(words[y].Substring(0, words[y].Length - 1));
i = y;
break;
}
else
{
if (words[y].StartsWith('"'.ToString()))
linked.Add(words[y].Substring(1));
}
}
newWords.Add(string.Join(separator, linked.ToArray()));
linked.Clear();
}
else
newWords.Add(words[i]);
}
return newWords.ToArray();
}
}

Wednesday, July 14, 2010

Convert an ArrayList to delimited string in c#

//posted By Suresh
//Input = StrA!%StrC!%StrB!%StrD!%StrE";
//Output = StrE!%StrD!%StrC!%StrB!%StrA";

string InStr = "StrA!%StrC!%StrB!%StrD!%StrE";
string outStr = "";
string[] del = new string[] {"!%"};
string[] TempArr = InStr.Split(del, StringSplitOptions.None);
Array.Sort(TempArr);
Array.Reverse(TempArr);
outStr = string.Join(("!%",TempArr);