namespace FIMSpace
{
///
/// FM: Class which contains many helpful methods which operates
///
public static class FStringMethods
{
///
/// Converting int to strig
///
/// value to show
/// How many signs, value = 8, signs = 3 -> 008
public static string IntToString(this int value, int signs)
{
string output = value.ToString();
int missingZeros = signs - output.Length;
if (missingZeros > 0)
{
string missing = "0";
for (int i = 1; i < missingZeros; i++)
{
missing += 0;
}
output = missing + output;
}
return output;
}
///
/// Changing first letter to uppercase and rest to lowercase
///
public static string CapitalizeOnlyFirstLetter(this string text)
{
if (string.IsNullOrEmpty(text)) return text;
return text[0].ToString().ToUpper() + (text.Length > 1 ? text.Substring(1) : "");
}
///
/// Chanigng first letter to uppercase and rest without changes
///
public static string CapitalizeFirstLetter(this string text)
{
if (string.IsNullOrEmpty(text)) return text;
return text[0].ToString().ToUpper() + text.Substring(1);
}
///
/// Changing space signs to underline signs
///
public static string ReplaceSpacesWithUnderline(this string text)
{
if (text.Contains(" "))
{
text = text.Replace(" ", "_");
}
return text;
}
///
/// Returning string from end to the separator, for GetEndOfStringFromSeparator("ask/ddd/aaa", new char[] { '\\', '/' }) will return "aaa"
///
/// Base string, from which you need only part of it
/// separators array to define, when stop checking new char[] { '\\', '/' }
/// how many occurences of separator should happen
/// start checking from end or from start of base string
///
public static string GetEndOfStringFromSeparator(this string source, char[] separators, int which = 1, bool fromEnd = false)
{
bool separated = false;
int counter = 0;
int steps = 0;
int i = 0;
for (i = source.Length - 1; i >= 0; i--)
{
steps++;
for (int c = 0; c < separators.Length; c++)
{
if (source[i] == separators[c])
{
counter++;
if (counter == which)
{
i++;
separated = true;
break;
}
}
}
if (separated) break;
}
if (separated)
{
if (!fromEnd)
{
return source.Substring(0, source.Length - (steps));
}
else
{
return source.Substring(i, source.Length - i);
}
}
else
{
return "";
}
}
///
/// Same as above GetEndOfStringFromSeparator() but here you define separators as strings, so for GetEndOfStringFromStringSeparator("ask/ddd/aaa", new string[] { "ask" }) will return "/ddd/aaa"
///
public static string GetEndOfStringFromStringSeparator(this string source, string[] separators, int which = 1, bool rest = false)
{
bool separated = false;
int counter = 0;
int steps = 0;
int i = 0;
for (i = 0; i < source.Length; i++)
{
steps++;
for (int c = 0; c < separators.Length; c++)
{
if (i + separators[c].Length > source.Length) break;
if (source.Substring(i, separators[c].Length) == separators[c])
{
counter++;
if (counter == which)
{
i++;
i += separators[c].Length - 1;
separated = true;
break;
}
}
}
if (separated) break;
}
if (separated)
{
if (rest)
{
return source.Substring(0, source.Length - (steps));
}
else
{
return source.Substring(i, source.Length - i);
}
}
else
{
return "";
}
}
}
}