Basit ve etkili kullanıma açık C# String fonksiyonları.
public class StringFunctions
{
public static string PregReplace(string input, string[] pattern, string[] replacements)
{
if (replacements.Length != pattern.Length)
throw new ArgumentException("Replacement and Pattern Arrays must be balanced");
for (int i = 0; i < pattern.Length; i++)
{
input = Regex.Replace(input, pattern[i], replacements[i]);
}
return input;
}
///
///
///
///
///
public static string RemoveAlphaCharacter(string String)
{
return Regex.Replace(String, "[a-zçığüşöA-ZÇIÜŞÖĞİ()%:;&]", "").Trim();
}
///
///
///
///
///
public static string RemoveAllAlphaCharacter(string String)
{
return Regex.Replace(String, "[a-zçığüşöA-ZÇIÜŞÖĞİ()%:;&.,!]", "").Trim();
}
///
///
///
///
///
public static int Asc(char theChar)
{
int num;
var num2 = Convert.ToInt32(theChar);
if (num2 < 0x80)
{
return num2;
}
try
{
byte[] buffer;
var fileIoEncoding = Encoding.Default;
var chars = new[] { theChar };
if (fileIoEncoding.IsSingleByte)
{
buffer = new byte[1];
return buffer[0];
}
buffer = new byte[2];
if (fileIoEncoding.GetBytes(chars, 0, 1, buffer, 0) == 1)
{
return buffer[0];
}
if (BitConverter.IsLittleEndian)
{
var num4 = buffer[0];
buffer[0] = buffer[1];
buffer[1] = num4;
}
num = BitConverter.ToInt16(buffer, 0);
}
catch (Exception exception)
{
throw exception;
}
return num;
}
///
///
///
///
///
private static double ConvertDouble(string val)
{
try
{
if ((val.Length - (val.LastIndexOf(",", StringComparison.Ordinal) + 1)) <= 2)
{
if (val.IndexOf(".", StringComparison.Ordinal) < val.IndexOf(",", StringComparison.Ordinal))
{
val = val.Replace(".", "");
}
val = val.Replace(",", ".");
}
return double.Parse(val, NumberStyles.Currency, new CultureInfo("en-US"));
}
catch (Exception)
{
return double.Parse(val.Replace(".", "").Replace(",", "."), NumberStyles.Currency, new CultureInfo("en-US"));
}
}
///
///
///
///
///
///
public static string SavingPercent(string xprice, string xsavingPrice)
{
double price = ConvertDouble(xprice);
double savingPrice = ConvertDouble(xsavingPrice);
double total = price + savingPrice;
return Math.Round(((total * 100) - (price * 100)) / total).ToString(CultureInfo.InvariantCulture);
}
///
/// Retrieves the path part of a full filename
///
/// string
/// C:\Folder\file.exe -> C:\Folder\
/// string
public static string GetPath(string full)
{
for (int i = full.Length - 1; i >= 0; i += -1)
{
if (full.Substring(i, 1) == "\\" || full.Substring(i, 1) == "/")
{
//Find the rightmost \ or /, which should be cut off the file part
return full.Substring(0, i) + "\\";
}
}
return "";
}
//Retrieves the extension of the file from a filename
//C:\Folder\file.exe - > exe
//OR
//file.exe -> exe
public static string GetExtension(string full)
{
for (var i = full.Length - 1; i >= 0; i += -1)
{
var index = full.IndexOf(".", i, StringComparison.Ordinal);
//Find the right most . (a different technique than GetPath)
if (index > -1)
{
return full.Substring(index);
}
}
return "";
}
//Removes extension from a filename
//C:\Folder\file.exe - > C:\Folder\file
//OR
//file.exe -> file
public static string RemoveExtension(string full)
{
return full.Substring(0, full.Length - GetExtension(full).Length);
}
//Removes path from a filename
//C:\Folder\file.exe - > file.exe
public static string GetFullName(string full)
{
//Cut off everything up to the path
return full.Substring(GetPath(full).Length);
}
//Capitalizes a word or sentence
//word -> Word
//OR
//this is a sentence -> This is a sentence
public static string Capitalize(string String)
{
if (String.Length == 1) return String.ToUpper();
//one letter just return it capitalized
//cut the first letter and capitalize it and then add the rest
return String.Substring(0, 1).ToUpper() + String.Substring(1);
}
//checks whether a word or sentence is capitalized
//Word -> True
//OR
//This is a sentence -> True
public static bool IsCapitalized(string String)
{
//compare the ascii value of the first letter and a capitalized first letter
return Asc(Convert.ToChar(String.Substring(0, 1))) == Asc(Convert.ToChar(String.Substring(0, 1).ToUpper()));
}
//checks whether a word or char is in lower case
//word -> True
//Word -> False
public static bool IsLower(string String)
{
for (int i = 0; i <= String.Length - 1; i++)
{
//compare the asc values
if (Asc(Convert.ToChar(String.Substring(i, 1))) != Asc(Convert.ToChar(String.Substring(i, 1).ToLower()))) return false;
}
return true;
}
//checks whether a word or char is in upper case
//word -> False
//Word -> False
//WORD -> True
public static bool IsUpper(string String)
{
for (int i = 0; i <= String.Length - 1; i++)
{
//compare asc values
if (Asc(Convert.ToChar(String.Substring(i, 1))) != Asc(Convert.ToChar(String.Substring(i, 1).ToUpper()))) return false;
}
return true;
}
//swaps the cases in a word
//word -> WORD
//Word -> wORD
//WoRd -> wOrD
public static string SwapCases(string String)
{
var ret = new StringBuilder();
//StringBuilder used to be faster on bigger strings
for (var i = 0; i <= String.Length - 1; i++)
{
ret.Append(IsUpper(String.Substring(i, 1))
? String.Substring(i, 1).ToLower()
: String.Substring(i, 1).ToUpper());
}
//Take of ".ToString" if you prefer it in StringBuilder form (and change the function to StringBuilder also)
return ret.ToString();
}
//Alternates cases between letters of a string, first letter's case stays the same
//Hi -> Hi
//longstring -> lOnGsTrInG
public static string AlternateCases(string String)
{
if (String.Length == 1) return String;
//Can't alternate if only one letter
var upper = !IsUpper(String.Substring(0, 1));
//start alternation depending on the first letter's case
var ret = new StringBuilder(String.Substring(0, 1));
for (var i = 1; i <= String.Length - 1; i++)
{
ret.Append(upper ? String.Substring(i, 1).ToUpper() : String.Substring(i, 1).ToLower());
//switch to capitalize or not
upper = !upper;
}
return ret.ToString();
}
//Checks to see if a string has alternate cases
//lOnGsTrInG -> True
public static bool IsAlternateCases(string String)
{
if (String.Length == 1) return false;
//One letter strings can't be alternate cases
bool upper = !IsUpper(String.Substring(0, 1));
//Same structure as AlternateCases function, depends on the first letter
for (int i = 1; i <= String.Length - 1; i++)
{
if (upper)
{
if (!IsUpper(String.Substring(i, 1))) return false;
}
else
{
if (IsUpper(String.Substring(i, 1))) return false;
}
upper = !upper;
}
return true;
}
//Checks for mixed upper and lower cases
//string -> False
//String -> True
public static bool IsMixedCases(string String)
{
bool upper = false;
for (int i = 0; i <= String.Length - 1; i++)
{
//look for the first letter and see if it's capitalized or not
if (IsLetters(String.Substring(i, 1)))
{
upper = IsUpper(String.Substring(i, 1));
}
}
if (String.Length == 1) return false;
for (int i = 1; i <= String.Length - 1; i++)
{
//something has a different case, then it's now mixed, ignores numbers and others
if (IsUpper(String.Substring(i, 1)) != upper && IsLetters(String.Substring(i, 1))) return true;
}
return false;
}
//Counts total number of a char or chars in a string
//hello, l -> 2
//hello, he -> 1
public static int CountTotal(string String, string chars)
{
var count = 0;
for (var i = 0; i <= String.Length - 1; i += chars.Length)
{
//The way String.Compare works, "Not CBool" converts it to a traditional True/False
//Compare is used to compare ignoring capitalization
if (!(i + chars.Length > String.Length) && String.Compare(String.Substring(i, chars.Length), chars, StringComparison.OrdinalIgnoreCase) == 1)
{
count += 1;
}
}
return count;
}
//Removes vowels from a word
//remove -> rmv
public static string RemoveVowels(string String)
{
var ret = new StringBuilder();
for (var i = 0; i <= String.Length - 1; i++)
{
if (!(String.Compare(String.Substring(i, 1), "a", StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(String.Substring(i, 1), "e", StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(String.Substring(i, 1), "i", StringComparison.OrdinalIgnoreCase) == 0 ||
String.Compare(String.Substring(i, 1), "o", StringComparison.OrdinalIgnoreCase) == 0 ||
(String.Compare(String.Substring(i, 1), "u", StringComparison.OrdinalIgnoreCase) == 0)))
{
//only add to the final string if it doesn't match the vowels
ret.Append(String.Substring(i, 1));
}
}
return ret.ToString();
}
//Checks to see if a word contains vowels
//hello -> True
//rmv -> False
public static bool HasVowels(string String)
{
for (int i = 0; i <= String.Length - 1; i++)
{
if ((String.Compare(String.Substring(i, 1), "a", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(String.Substring(i, 1), "e", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(String.Substring(i, 1), "i", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(String.Substring(i, 1), "o", StringComparison.OrdinalIgnoreCase) == 0 || String.Compare(String.Substring(i, 1), "u", StringComparison.OrdinalIgnoreCase) == 0))
{
//if something equals one of the vowels then it has vowels
return true;
}
}
return false;
}
//Checks if string is nothing but spaces
//" " -> True
public static bool IsSpaces(string String)
{
for (int i = 0; i <= String.Length - 1; i++)
{
if (Asc(Convert.ToChar(String.Substring(i, 1))) != 32)
{
//32 is the asc value of a space
return false;
}
}
return true;
}
//Checks if string has only numbers
//(Since parameter is String instead of Object, it should be faster)
//12453 -> True
//234d3 -> False
public static bool IsNumeric(string String)
{
for (int i = 0; i <= String.Length - 1; i++)
{
if (!(Asc(Convert.ToChar(String.Substring(i, 1))) >= 48 && Asc(Convert.ToChar(String.Substring(i, 1))) <= 57))
{
//asc value range of numbers
return false;
}
}
return true;
}
//Checks if the string contains numbers
//hello -> False
//h3llo -> True
public static bool HasNumbers(string String)
{
return Regex.IsMatch(String, "\\d+");
}
//Checks if string is numbers and letters
//Test1254 -> True
//$chool -> False
public static bool IsAlphaNumberic(string String)
{
for (int i = 0; (i
<= (String.Length - 1)); i++)
{
if (!(IsNumeric(String.Substring(i, 1)) || (((Asc(Convert.ToChar(String.Substring(i, 1))) >= 65) && (Asc(Convert.ToChar(String.Substring(i, 1))) <= 90)) || ((Asc(Convert.ToChar(String.Substring(i, 1))) >= 97) && (Asc(Convert.ToChar(String.Substring(i, 1))) <= 122)))))
{
return false;
}
}
return true;
}
//Checks if a string contains only letters
//Hi -> True
//Hi123 -> False
public static bool IsLetters(string String)
{
if (IsAlphaNumberic(String))
{
//Only numbers and letters, good
if (!IsNumeric(String))
{
//no numbers, good
return true;
}
}
return false;
}
//Checks if string is a valid emailaddress-format
//name@place.com -> True
//hahaimfaking -> False
//(Function works assuming the last part is no bigger than 3 letters (com, net, org))
public static bool IsEmailAddress(string String)
{
if (String.IndexOf("@", StringComparison.Ordinal) > -1)
{
for (int i = String.Length - 1; i >= 0; i += -1)
{
if (String.Substring(i, 1) != ".") continue;
if (String.Length - i <= 4)
{
return true;
}
}
}
return false;
}
//Returns all the locations of a char in a string
//Hello, l -> 2, 3
//Hello, o -> 4
public static int[] IndexesOf(string String, string Char)
{
var inx = new ArrayList();
for (var i = 0; i <= String.Length - 1; i++)
{
//for every location found add it
if (String.Substring(i, 1) == Char) inx.Add(i);
}
var final = new int[inx.Count];
inx.CopyTo(final);
//convert the ArrayList to an Integer array
inx.Clear();
return final;
}
//Return a rating for how strong the string is as a password
//Max rating is 100
//Credits for original function to D. Rijmenants, this is just a vb.net version
//If there are problems with copyright or whatever, contact me and i'll delete this
public static int PasswordStrength(string String)
{
var uc = false;
var lc = false;
double total = String.Length * 3;
for (var i = 1; i <= String.Length - 1; i++)
{
//contains uppercases
if (Asc(Convert.ToChar(String.Substring(i, 1))) >= 65 && Asc(Convert.ToChar(String.Substring(i, 1))) <= 92) uc = true;
}
for (var i = 1; i <= String.Length - 1; i++)
{
//contains lowercases
if (Asc(Convert.ToChar(String.Substring(i, 1))) >= 97 & Asc(Convert.ToChar(String.Substring(i, 1))) <= 122) lc = true;
}
if (uc & lc) total *= 1.2;
for (var i = 1; i <= String.Length - 1; i++)
{
if (Asc(Convert.ToChar(String.Substring(i, 1))) >= 48 & Asc(Convert.ToChar(String.Substring(i, 1))) <= 57)
{
//contains numbers
if (uc | lc) total *= 1.4;
break;
}
}
for (var i = 1; i <= String.Length - 1; i++)
{
if (Asc(Convert.ToChar(String.Substring(i, 1))) <= 47 | Asc(Convert.ToChar(String.Substring(i, 1))) >= 123 | (Asc(Convert.ToChar(String.Substring(i, 1))) >= 58 & Asc(Convert.ToChar(String.Substring(i, 1))) <= 64))
{
//contains some extra symbols
total *= 1.5;
break;
}
}
if (total > 100) total = 100;
//make sure not over 100
return (int)total;
}
//Gets the char in a string at a given position, but from right to left
//string, 0 -> g
public static char CharRight(string String, int position)
{
return Convert.ToChar(String.Substring(String.Length - position - 1, 1));
}
//Gets the char in a string at a given position from the given starting point, reads left to right
//string, 0, 2 -> r
public static char CharMid(string String, int position, int start)
{
return Convert.ToChar(!(start + position > String.Length) ? String.Substring(start + position, 1) : "");
}
//Inserts a separator after every letter
//hello, - -> h-e-l-l-o
public static string InsertSeparator(string String, string separator)
{
var final = new StringBuilder();
for (var i = 0; i <= String.Length - 1; i++)
{
//after every letter add the separator
final.Append(String.Substring(i, 1) + separator);
}
//Cut out last separator (h-e-l-l-o- -> h-e-l-l-o)
return final.ToString().Substring(0, final.Length - 1);
}
//Inserts a separator after every Count letters
//hello, -, 2 -> he-ll-o
public static string InsertSeparatorEvery(string String, string separator, int count)
{
var final = new StringBuilder();
for (var i = 0; i <= String.Length - 1; i += count)
{
//every so steps add the separator
if (!(i + count > String.Length))
{
final.Append(String.Substring(i, count) + separator);
}
else
{
final.Append(String.Substring(i) + separator);
}
}
//Cut out last separator
return final.ToString().Substring(0, final.Length - 1);
}
//Inserts a separator at given position
//hello, -, 3 -> hel-lo
public static string InsertSeparatorAt(string String, string separator, int position)
{
//split the string at that position and add the separator
return String.Substring(0, position) + separator + String.Substring(position);
}
//Function that works the same way as the default Substring, but
//it takes Start and End parameters instead of Start and Length
public static string Substring(string String, int start, int end)
{
if (start > end)
{
//If start is after the end then just flip the values
start = start ^ end;
end = start ^ end;
start = start ^ end;
}
if (end > String.Length) end = String.Length;
//if the end is outside of the string, just make it the end of the string
return String.Substring(start, end - start);
}
//Reverses a string
//Hello -> olleH
public static string Reverse(string String)
{
var final = new StringBuilder();
for (var i = String.Length - 1; i >= 0; i += -1)
{
//read the string backwards
final.Append(String.Substring(i, 1));
}
return final.ToString();
}
}




0 yorum:
Yorum Gönder