Hi Dosto
Please find StringHelper class for working on string related functionalities
Please find StringHelper class for working on string related functionalities
public static class StringHelper { /// <summary> /// Receives string and retruns the string with its letters reversed. /// </summary> /// <param name="s"></param> /// <returns></returns> public static string ReverseString(string s) { char[] arr = s.ToCharArray(); Array.Reverse(arr); return new string(arr); } //Convert string to PorperCase public static String PCase(String strParam) { String strProper = strParam.Substring(0, 1).ToUpper(); strParam = strParam.Substring(1).ToLower(); String strPrev = ""; for (int iIndex = 0; iIndex < strParam.Length; iIndex++) { if (iIndex > 1) { strPrev = strParam.Substring(iIndex - 1, 1); } if (strPrev.Equals(" ") || strPrev.Equals("\t") || strPrev.Equals("\n") || strPrev.Equals(".")) { strProper += strParam.Substring(iIndex, 1).ToUpper(); } else { strProper += strParam.Substring(iIndex, 1); } } return strProper; } // Replace string with a found string in the source string public static String Replace(String strText, String strFind, String strReplace) { int iPos = strText.IndexOf(strFind); String strReturn = ""; while (iPos != -1) { strReturn += strText.Substring(0, iPos) + strReplace; strText = strText.Substring(iPos + strFind.Length); iPos = strText.IndexOf(strFind); } if (strText.Length > 0) strReturn += strText; return strReturn; } // Trim the string to contain only a single whitepace between words public static String ToSingleSpace(String strParam) { int iPosition = strParam.IndexOf(" "); if (iPosition == -1) { return strParam; } else { return ToSingleSpace(strParam.Substring(0, iPosition) + strParam.Substring(iPosition + 1)); } } // Count the number of occurrences of a substring in a source string // case sensitive public static int CharCount(String strSource, String strToCount) { int iCount = 0; int iPos = strSource.IndexOf(strToCount); while (iPos != -1) { iCount++; strSource = strSource.Substring(iPos + 1); iPos = strSource.IndexOf(strToCount); } return iCount; } // Count the number of occurrences of a substring in a source string // case insensitive public static int CharCount(String strSource, String strToCount, bool IgnoreCase) { if (IgnoreCase) { return CharCount(strSource.ToLower(), strToCount.ToLower()); } else { return CharCount(strSource, strToCount); } } /// <summary> ///Reverse the String passed /// </summary> /// <param name="strParam"></param> /// <returns></returns> public static String Reverse(String strParam) { if (strParam.Length == 1) { return strParam; } else { return Reverse(strParam.Substring(1)) + strParam.Substring(0, 1); } } // Get a number of characters of a string from the beginning public static String Left(String strParam, int iLen) { if (iLen > 0) return strParam.Substring(0, iLen); else return strParam; } // Get a number of characters of a string from then end public static String Right(String strParam, int iLen) { if (iLen > 0) return strParam.Substring(strParam.Length - iLen, iLen); else return strParam; } // Test if the string is Palindrome public static bool IsPalindrome(String strParam) { int iLength, iHalfLen; iLength = strParam.Length - 1; iHalfLen = iLength / 2; for (int iIndex = 0; iIndex <= iHalfLen; iIndex++) { if (strParam.Substring(iIndex, 1) != strParam.Substring(iLength - iIndex, 1)) { return false; } } return true; } /// <summary> /// Returns a string of all characters to the left of the search character.
/// If the character cannot be found in the source string, the entire string is
/// returned. The rationale here is that the entire string is scanned for the search
/// character, and, not being found, the method returns all the characters scanned. /// </summary> /// <param name="src"></param> /// <param name="c"></param> /// <returns></returns> public static string LeftOf(string src, char c) { string ret = src; int idx = src.IndexOf(c); if (idx != -1) { ret = src.Substring(0, idx); } return ret; } /// <summary> /// Returns a string of all characters to the left of the nth occurrence of the
/// search character. If the character cannot be found in the source string,
/// the entire string is returned. The rationale here is that the entire string is
/// scanned for the search character, and, not being found, the method returns all
/// the characters scanned. /// </summary> /// <param name="src"></param> /// <param name="c"></param> /// <param name="n"></param> /// <returns></returns> public static string LeftOf(string src, char c, int n) { string ret = src; int idx = -1; while (n > 0) { idx = src.IndexOf(c, idx + 1); if (idx == -1) { break; } } if (idx != -1) { ret = src.Substring(0, idx); } return ret; } /// <summary> /// Returns a string of all characters to the right of the search character.
/// If the character cannot be found in the source string, an empty string is returned.
/// The rationale here is that the entire string is scanned for the search character,
/// but until the search character is found, these characters are ignored. Therefore,
/// if the search character is not found, the method returns an empty string. /// </summary> /// <param name="src"></param> /// <param name="c"></param> /// <returns></returns> public static string RightOf(string src, char c) { string ret = String.Empty; int idx = src.IndexOf(c); if (idx != -1) { ret = src.Substring(idx + 1); } return ret; } /// <summary> /// Returns a string of all characters to the right of the nth occurrence of
/// the search character /// </summary> /// <param name="src"></param> /// <param name="c"></param> /// <param name="n"></param> /// <returns></returns> public static string RightOf(string src, char c, int n) { string ret = String.Empty; int idx = -1; while (n > 0) { idx = src.IndexOf(c, idx + 1); if (idx == -1) { break; } --n; } if (idx != -1) { ret = src.Substring(idx + 1); } return ret; } /// <summary> /// Returns a string of all characters to the left of the rightmost occurrence of
/// the search character. If the character cannot be found in the source string,
/// the entire string is returned. The rationale here is that the entire string
/// is scanned for the search character, and, not being found,
/// the method returns all the characters scanned. /// </summary> /// <param name="src"></param> /// <param name="c"></param> /// <returns></returns> public static string LeftOfRightmostOf(string src, char c) { string ret = src; int idx = src.LastIndexOf(c); if (idx != -1) { ret = src.Substring(0, idx); } return ret; } /// <summary> /// Returns the string that is between the two specified search characters.
/// The returned string excludes the search characters. If the starting character
/// is not found, an empty string is returned. If the ending character is
/// not found after the starting character, an empty string is returned. /// </summary> /// <param name="src"></param> /// <param name="start"></param> /// <param name="end"></param> /// <returns></returns> public static string Between(string src, char start, char end) { string ret = String.Empty; int idxStart = src.IndexOf(start); if (idxStart != -1) { ++idxStart; int idxEnd = src.IndexOf(end, idxStart); if (idxEnd != -1) { ret = src.Substring(idxStart, idxEnd - idxStart); } } return ret; } /// <summary> /// Returns 0 if no occurrences of the search character are found in the source
/// string. /// </summary> /// <param name="src"></param> /// <param name="find"></param> /// <returns></returns> public static int Count(string src, char find) { int ret = 0; foreach (char s in src) { if (s == find) { ++ret; } } return ret; } /// <summary> /// Returns '\0' if the string is empty. /// </summary> /// <param name="src"></param> /// <returns></returns> public static char Rightmost(string src) { char c = '\0'; if (src.Length > 0) { c = src[src.Length - 1]; } return c; } /// <summary> /// Check particular string present in provided string or not /// </summary> /// <param name="source"></param> /// <param name="toCheck"></param> /// <param name="comp"></param> /// <returns></returns> public static bool Contains(this string source, string toCheck, StringComparison comp) { return source.IndexOf(toCheck, comp) >= 0; } /// <summary> /// Check value is present or not /// </summary> /// <param name="value"></param> /// <returns></returns> public static bool HasValue(this string value) { return !String.IsNullOrWhiteSpace(value); } /// <summary> /// Get Integer value from string /// </summary> /// <param name="value"></param> /// <param name="defaultValue"></param> /// <returns></returns> public static int GetIntValue(this string value, int defaultValue = 0) { int val; return string.IsNullOrWhiteSpace(value) ? defaultValue : int.TryParse(value, out val) ? val : defaultValue; } }
No comments:
Post a Comment