History

GSF.Core + GSF.Core.Shared: (1) Add StringParser class to GSF.Core. (2) Add new StringExtensions and CharExtensions functions

Stephen Jenks committed 9 years ago

Commit c80b8711 parent f0f8c6b4 files · +1467 −0Browse files

Showing the changes to Source/Libraries/GSF.Core.Shared/StringExtensions.cs (1 of 4 files). Show all

Unified · Split

Source/Libraries/GSF.Core.Shared/StringExtensions.cs+439−0
@@ -1666,5 +1666,444 @@ namespace GSF · 1665 unmodified lines
16661666 {
16671667 return Uri.EscapeDataString(value);
16681668 }
1669+
1670+ //********** New methods July 2017, To Be Tested ********************
1671+
1672+ /// <summary>
1673+ /// Removes one or more instances of a specified string from the beginning of a string.
1674+ /// </summary>
1675+ /// <param name="value">The string to process</param>
1676+ /// <param name="stringToRemove">The string to remove</param>
1677+ /// <param name="matchCase">Set to <c>false</c> for case insensitive search</param>
1678+ /// <returns>A string with <paramref name="stringToRemove"/> deleted from the beginning</returns>
1679+ public static string RemoveLeadingString(this string value, string stringToRemove, bool matchCase = true)
1680+ {
1681+ if (string.IsNullOrEmpty(value))
1682+ return string.Empty;
1683+
1684+ if (string.IsNullOrEmpty(stringToRemove))
1685+ return value;
1686+
1687+ StringComparison scCase = (matchCase) ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
1688+
1689+ int i = value.IndexOf(stringToRemove, scCase);
1690+ if (i < 0)
1691+ return value; //handle the "do nothing" case quickly
1692+
1693+ while (value.StartsWith(stringToRemove, scCase))
1694+ {
1695+ if (value.Length <= stringToRemove.Length)
1696+ return string.Empty;
1697+
1698+ value = value.Substring(stringToRemove.Length);
1699+ }
1700+
1701+ return value;
1702+ }
1703+
1704+ /// <summary>
1705+ /// Removes one or more instances of a specified char from the beginning of a string.
1706+ /// </summary>
1707+ /// <param name="value">The string to process</param>
1708+ /// <param name="charToRemove">The char to remove</param>
1709+ /// <param name="matchCase">Set to <c>false</c> for case insensitive search</param>
1710+ /// <returns>A string with <paramref name="charToRemove"/> deleted from the beginning</returns>
1711+ public static string RemoveLeadingString(this string value, char charToRemove, bool matchCase = true)
1712+ {
1713+
1714+ if (string.IsNullOrEmpty(value))
1715+ return string.Empty;
1716+
1717+ if (charToRemove == 0)
1718+ return value;
1719+
1720+ string result = value;
1721+ if (!matchCase)
1722+ {
1723+ value = value.ToLower();
1724+ charToRemove = charToRemove.ToLower();
1725+ }
1726+
1727+ //handle the "do nothing" case quickly
1728+ if (value[0] != charToRemove) return result;
1729+
1730+ for (int i = 1; i < value.Length; i++)
1731+ {
1732+ if (value[i] != charToRemove)
1733+ {
1734+ return result.Substring(i - 1);
1735+ }
1736+ }
1737+ //all the characters are to be removed.
1738+ return string.Empty;
1739+ }
1740+
1741+ /// <summary>
1742+ /// Assures that numeric value is a well formed number
1743+ /// Adds a leading zero in front of a decimal, if present
1744+ /// </summary>
1745+ /// <param name="value">The string to process</param>
1746+ /// <param name = "AssureParseDouble">Set to TRUE to assure that value parses to Double</param>
1747+ /// <returns>A string with deleted from the beginning</returns>
1748+ /// <remarks>Note: AssureParseDouble also removes trailing zeros following a decimal which implies loss of precision in the value</remarks>
1749+ public static string RemoveLeadingZeros(this string value, bool AssureParseDouble = false)
1750+ {
1751+ if (string.IsNullOrEmpty(value))
1752+ return "0";
1753+
1754+ value = value.Trim();
1755+
1756+ if (value.Length == 0)
1757+ return "0";
1758+
1759+ if (AssureParseDouble)
1760+ {
1761+ double test = 0.0;
1762+ if (!double.TryParse(value, out test))
1763+ return "0";
1764+
1765+ return test.ToString();
1766+ }
1767+
1768+ bool isNeg = false;
1769+
1770+ if (value.Substring(0, 1) == "-")
1771+ {
1772+ isNeg = true;
1773+ value = value.Substring(1);
1774+ }
1775+
1776+ while (value.Substring(0, 1) == "0")
1777+ {
1778+ if (value.Length <= 1)
1779+ return "0";
1780+
1781+ value = value.Substring(1);
1782+ }
1783+
1784+ if (value.Substring(0, 1) == ".")
1785+ value = "0" + value;
1786+
1787+ if (isNeg && value != "0.0")
1788+ value = "-" + value;
1789+
1790+ return value;
1791+ }
1792+
1793+ /// <summary>
1794+ /// Removes one or more instances of a string from the end of a string
1795+ /// </summary>
1796+ /// <param name="value">The string to process</param>
1797+ /// <param name="stringToRemove">The string to remove</param>
1798+ /// <param name="matchCase">Set to <c>false</c> for case insensitive search</param>
1799+ /// <returns>A string with <paramref name="stringToRemove"/> deleted from the end</returns>
1800+ public static string RemoveTrailingString(this string value, string stringToRemove, bool matchCase = true)
1801+ {
1802+ if (string.IsNullOrEmpty(value))
1803+ return string.Empty;
1804+
1805+ if (string.IsNullOrEmpty(stringToRemove))
1806+ return value;
1807+
1808+ StringComparison scCase = (matchCase) ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
1809+
1810+ int i = value.IndexOf(stringToRemove, scCase);
1811+
1812+ if (i < 0)
1813+ return value; //handle the "do nothing" case quickly
1814+
1815+ while (value.EndsWith(stringToRemove, scCase))
1816+ {
1817+ if (value.Length <= stringToRemove.Length)
1818+ return string.Empty;
1819+
1820+ value = value.Substring(0, value.Length - stringToRemove.Length);
1821+ }
1822+
1823+ return value;
1824+ }
1825+
1826+ /// <summary>
1827+ /// Removes one or more instances of a character from the end of a string
1828+ /// </summary>
1829+ /// <param name="value">The string to process</param>
1830+ /// <param name="charToRemove">The char to remove</param>
1831+ /// <param name="matchCase">Set to <c>false</c> for case insensitive search</param>
1832+ /// <returns>A string with <paramref name="charToRemove"/> deleted from the end</returns>
1833+ public static string RemoveTrailingString(this string value, char charToRemove, bool matchCase = true)
1834+ {
1835+ if (string.IsNullOrEmpty(value))
1836+ return string.Empty;
1837+
1838+ if (charToRemove == 0)
1839+ return value;
1840+
1841+ string result = value;
1842+ if (!matchCase)
1843+ {
1844+ value = value.ToLower();
1845+ charToRemove = charToRemove.ToLower();
1846+ }
1847+
1848+ //handle the "do nothing" case quickly
1849+ if (value[value.Length - 1] != charToRemove) return result;
1850+
1851+ for (int i = value.Length - 1; i > -1; i--)
1852+ {
1853+ if (value[i] != charToRemove)
1854+ {
1855+ return result.Substring(0, i + 1);
1856+ }
1857+ }
1858+ //all the characters are to be removed.
1859+ return string.Empty;
1860+ }
1861+
1862+ /// <summary>
1863+ /// Returns a string consisting of a specified number of characters from the end of a string "to the left"
1864+ /// </summary>
1865+ /// <param name="value">The string to process</param>
1866+ /// <param name="length">The number of characters from the end of the string to return</param>
1867+ /// <returns>A string of length <paramref name="length" /></returns>
1868+ public static string SubstringEnd(this string value, int length)
1869+ {
1870+ if (string.IsNullOrEmpty(value) || length <= 0)
1871+ return string.Empty;
1872+
1873+ if (value.Length <= length)
1874+ return value;
1875+
1876+ return value.Substring(value.Length - length);
1877+ }
1878+
1879+ /// <summary>
1880+ /// Returns a string consisting of a specified number of characters to the left (previous chars) from the provided startIndex
1881+ /// </summary>
1882+ /// <param name="value">The string to process</param>
1883+ /// <param name="endIndex">The index in <paramref name="value" /> at the end of the desired returned string.</param>
1884+ /// <param name="length">The number of characters from the <paramref name="endIndex" /> of the string to return</param>
1885+ /// <returns>A string to the left of <paramref name="endIndex" /> of length up to <paramref name="length" /> </returns>
1886+ public static string SubstringPrevious(this string value, int endIndex, int length)
1887+ {
1888+ if (string.IsNullOrEmpty(value) || endIndex < 0 || length <= 0)
1889+ return string.Empty;
1890+
1891+ if (endIndex > value.Length - 1)
1892+ return string.Empty;
1893+
1894+ int startIndex = endIndex - length + 1;
1895+
1896+ if (startIndex < 0)
1897+ startIndex = 0;
1898+
1899+ return value.Substring(startIndex, endIndex - startIndex + 1);
1900+ }
1901+
1902+ /// <summary>
1903+ /// Searches a string from right to left for the next instance of a specified string.
1904+ /// </summary>
1905+ /// <param name="value">Input string to process.</param>
1906+ /// <param name="testString">The string to find.</param>
1907+ /// <param name="startIndex">The index in <paramref name="value"/> from which to begin looking for <paramref name="testString"/>. Typically length of <paramref name="value"/> minus 1."</param>
1908+ /// <returns>The start (or left most) index of <paramref name="testString"/> within <paramref name="value"/> or (-1) if not found.</returns>
1909+ public static int IndexOfPrevious(this string value, string testString, int startIndex = 0)
1910+ {
1911+ if (string.IsNullOrEmpty(value))
1912+ return -1;
1913+
1914+ if (string.IsNullOrEmpty(testString))
1915+ return -1;
1916+
1917+ if (startIndex < 0)
1918+ return -1;
1919+
1920+ if (startIndex > value.Length - 1 || startIndex - testString.Length + 1 < 0)
1921+ {
1922+ //return -1;
1923+ throw new ArgumentException("IndexOfPrevious startIndex Invalid " + startIndex.ToString());
1924+ }
1925+
1926+ //this is the fastest way to do this
1927+ string s = value.Reverse();
1928+ string v = testString.Reverse();
1929+ int i = s.IndexOf(v, s.Length - startIndex - 1);
1930+
1931+ if (i > -1)
1932+ return s.Length - i - testString.Length;
1933+
1934+ return -1;
1935+ }
1936+
1937+ /// <summary>
1938+ /// Searches a string from right to left for the next instance of a specified character.
1939+ /// </summary>
1940+ /// <param name="value">Input string to process.</param>
1941+ /// <param name="testChar">The char to find.</param>
1942+ /// <param name="startIndex">The index in <paramref name="value"/> from which to begin looking for <paramref name="testChar"/>. Typically length of <paramref name="value"/> minus 1."</param>
1943+ /// <returns>The index of <paramref name="testChar"/> within <paramref name="value"/> or (-1) if not found.</returns>
1944+ public static int IndexOfPrevious(this string value, char testChar, int startIndex = 0)
1945+ {
1946+ if (string.IsNullOrEmpty(value))
1947+ return -1;
1948+
1949+ if (testChar == 0)
1950+ return -1;
1951+
1952+ if (startIndex < 0)
1953+ return -1;
1954+
1955+ if (startIndex > value.Length - 1)
1956+ {
1957+ throw new ArgumentException("IndexOfPrevious startIndex Invalid " + startIndex.ToString());
1958+ }
1959+
1960+ for (int j = startIndex; j > -1; j--)
1961+ {
1962+ if (value[j] == testChar)
1963+ return j;
1964+ }
1965+ return -1;
1966+ }
1967+
1968+ /// <summary>
1969+ /// Finds the first index that is NOT included in testChars
1970+ /// </summary>
1971+ /// <param name="value">String to process</param>
1972+ /// <param name="anyOf">the characters use to test</param>
1973+ /// <param name="startIndex">the index at which to begin testing <paramref name="value"/></param>
1974+ /// <returns>The first index of a character NOT included in <paramref name="anyOf"/>. </returns>
1975+ public static int IndexOfNot(this string value, char[] anyOf, int startIndex = 0)
1976+ {
1977+ if (string.IsNullOrEmpty(value))
1978+ return -1;
1979+
1980+ if (anyOf == null || anyOf[0] == 0)
1981+ return -1;
1982+
1983+ if (startIndex >= value.Length)
1984+ return -1;
1985+
1986+ for (int i = startIndex; i < value.Length; i++)
1987+ {
1988+ if (value.IndexOfAny(anyOf) == -1)
1989+ return i;
1990+ }
1991+
1992+ return -1;
1993+ }
1994+
1995+ /// <summary>
1996+ /// Counts the total number of the occurrences of string within a string
1997+ /// </summary>
1998+ /// <param name="value">Input string to process.</param>
1999+ /// <param name="testString">String to be counted.</param>
2000+ /// <param name="startIndex">The index at which to begin <paramref name="value"/></param>
2001+ /// <param name="matchCase">Set to <c>false</c> for case insensitive search</param>
2002+ /// <returns>Total number of the occurrences of <paramref name="testString" /> in the given string.</returns>
2003+ public static int StringCount(this string value, string testString, int startIndex = 0, bool matchCase = true)
2004+ {
2005+ if (string.IsNullOrEmpty(value))
2006+ return 0;
2007+
2008+ //error - ambiguous safe case
2009+ if (startIndex < 0)
2010+ return 0;
2011+
2012+ if (startIndex >= value.Length)
2013+ {
2014+ throw new ArgumentException("StringCount startIndex Invalid " + startIndex.ToString());
2015+ }
2016+
2017+ if (string.IsNullOrEmpty(testString))
2018+ return 0;
2019+
2020+ int total = 0;
2021+ StringComparison scCase = (matchCase) ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
2022+ int k = value.IndexOf(testString, startIndex, scCase);
2023+
2024+ while (k > -1)
2025+ {
2026+ total++;
2027+ k = value.IndexOf(testString, k + testString.Length, scCase);
2028+ }
2029+ return total;
2030+ }
2031+
2032+ /// <summary>
2033+ /// Unwraps quotes similar to Excel. However, a little more predictable for unusual edge cases.
2034+ /// </summary>
2035+ /// <param name="value">The string to process</param>
2036+ /// <param name="quoteChar">The quote char to use. Default is double quote. Due to trimming, quoteChar of whitespace is not allowed.</param>
2037+ /// <remarks>
2038+ /// To remove all quote chars at beginning and end of a string regardless of match use .Trim('\"') It's faster.
2039+ /// Three consecutive quotes assures a singe quote char in output.
2040+ /// </remarks>
2041+ /// <returns>The sent string with matched surrounding double quotes removed.</returns>
2042+ public static string quoteUnwrap(this string value, char quoteChar = '\"')
2043+ {
2044+ if (string.IsNullOrEmpty(value))
2045+ return string.Empty;
2046+
2047+ string s = value.Trim();
2048+ if (s.Length <= 1)
2049+ return value;
2050+
2051+ if (quoteChar == ' ')
2052+ throw new ArgumentOutOfRangeException("quoteChar cannot be space (Character 32).");
2053+
2054+ string assuredQuote = new string(quoteChar, 3);
2055+ const char marker = (char)2;
2056+ string markerString = new string(marker, 1);
2057+ string quoteString = new string(quoteChar, 1);
2058+
2059+ s = s.Replace(assuredQuote, markerString);
2060+
2061+ int quoteCount = s.CharCount(quoteChar);
2062+ if (quoteCount > 2 && quoteCount % 2 == 0) //even number and more than two quotes, assume that quotes are important to retain.
2063+ {
2064+ return s.Replace(markerString, quoteString);
2065+ }
2066+
2067+ while (s.IndexOf(quoteChar) == 0) //work off front of string -- and reduce it.
2068+ {
2069+ if (s[s.Length - 1] == quoteChar) //have matching end quote
2070+ {
2071+ s = s.Substring(1, s.Length - 2);
2072+ int nq = s.IndexOf(quoteChar);
2073+ int ns = s.IndexOf(' ');
2074+ if (nq > -1 && ns > -1 && nq > ns) //allow white spaces between quotes -- but don't strip spaces following quotes
2075+ s.Trim();
2076+ if (s.Length <= 1)
2077+ return s.Replace(markerString, quoteString);
2078+ }
2079+ else
2080+ {
2081+ break;
2082+ }
2083+ }
2084+ return s.Replace(markerString, quoteString);
2085+ }
2086+
2087+ /// <summary>
2088+ ///
2089+ /// </summary>
2090+ /// <param name="value"></param>
2091+ /// <param name="quoteChars"></param>
2092+ /// <returns></returns>
2093+ public static string quoteUnwrap(this string value, char[] quoteChars)
2094+ {
2095+ if (string.IsNullOrEmpty(value))
2096+ return string.Empty;
2097+
2098+ if (quoteChars == null || quoteChars[0] == 0)
2099+ return value;
2100+
2101+ foreach (char c in quoteChars)
2102+ {
2103+ value = value.quoteUnwrap(c);
2104+ }
2105+
2106+ return value;
2107+ }
16692108 }
16702109 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t