本文整理汇总了C#中StringComparison类的典型用法代码示例。如果您正苦于以下问题:C# StringComparison类的具体用法?C# StringComparison怎么用?C# StringComparison使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StringComparison类属于命名空间,在下文中一共展示了StringComparison类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Replace
public static string Replace(this string source, string oldValue, string newValue, StringComparison comparisonType)
{
// from http://stackoverflow.com/a/22565605 with some adaptions
if (string.IsNullOrEmpty(oldValue))
{
throw new ArgumentNullException("oldValue");
}
if (source.Length == 0)
{
return source;
}
if (newValue == null)
{
newValue = string.Empty;
}
var result = new StringBuilder();
int startingPos = 0;
int nextMatch;
while ((nextMatch = source.IndexOf(oldValue, startingPos, comparisonType)) > -1)
{
result.Append(source, startingPos, nextMatch - startingPos);
result.Append(newValue);
startingPos = nextMatch + oldValue.Length;
}
result.Append(source, startingPos, source.Length - startingPos);
return result.ToString();
}
示例2: Replace
/// <summary>
/// 返回一个新字符串,其中当前实例中出现的所有指定字符串都替换为另一个指定的字符串。
/// </summary>
/// <param name="value">当前字符串。</param>
/// <param name="oldValue">要被替换的字符串。</param>
/// <param name="newValue">要替换出现的所有 oldValue 的字符串。</param>
/// <param name="comparisonType">指定搜索规则的枚举值之一。</param>
/// <returns>等效于当前字符串(除了 oldValue 的所有实例都已替换为 newValue 外)的字符串。</returns>
/// <exception cref="System.ArgumentNullException"><c>value</c> 为 null。</exception>
/// <exception cref="System.ArgumentNullException"><c>oldValue</c> 为 null。</exception>
/// <exception cref="System.ArgumentException"><c>oldValue</c> 是空字符串 ("")。</exception>
public static string Replace(this string value, string oldValue, string newValue,
StringComparison comparisonType)
{
if (value == null)
{
throw new ArgumentNullException("value", "未将对象引用设置到对象的实例。");
}
if (oldValue == null)
{
throw new ArgumentNullException("oldValue", "值不能为 null。");
}
if (oldValue.Length == 0)
{
throw new ArgumentException("字符串的长度不能为零。", "oldValue");
}
if (Enum.IsDefined(typeof(StringComparison), comparisonType) == false)
{
throw new ArgumentException("非法的 StringComparison。", "comparisonType");
}
while (true)
{
var index = value.IndexOf(oldValue, comparisonType);
if (index == -1)
{
return value;
}
value = value.Substring(0, index) + newValue + value.Substring(index + oldValue.Length);
}
}
示例3: Validate
/// <summary>
/// Validates the specified <paramref name="value"/> and throws an <see cref="InvalidEnumArgumentException"/>
/// exception if not valid.
/// </summary>
/// <param name="value">The value to validate.</param>
/// <param name="parameterName">Name of the parameter to use if throwing exception.</param>
public static void Validate(StringComparison value, string parameterName)
{
if (!IsDefined(value))
{
throw Error.InvalidEnumArgument(parameterName, (int)value, typeof(StringComparison));
}
}
示例4: Contains
/// <summary>
/// Determines whether a string contans another string.
/// </summary>
public static bool Contains(this string source, string value, StringComparison compareMode)
{
if (string.IsNullOrEmpty(source))
return false;
return source.IndexOf(value, compareMode) >= 0;
}
示例5: Validate
/// <summary>
/// Validates the specified <paramref name="value"/> and throws an <see cref="InvalidEnumArgumentException"/>
/// exception if not valid.
/// </summary>
/// <param name="value">The value to validate.</param>
/// <param name="parameterName">Name of the parameter to use if throwing exception.</param>
public static void Validate(StringComparison value, string parameterName)
{
if (!IsDefined(value))
{
throw new InvalidEnumArgumentException(parameterName, (int)value, _stringComparisonType);
}
}
示例6: EqualsSafely
/// <summary>
/// 确定此字符串是否与指定的 System.String 对象具有相同的值。 参数指定区域性、大小写以及比较所用的排序规则。
/// </summary>
/// <param name="this">当前 System.String 对象。</param>
/// <param name="value">要与此实例进行比较的字符串。</param>
/// <param name="comparisonType">枚举值之一,用于指定将如何比较字符串。</param>
/// <returns>如果 value 参数的值为空或与此字符串相同,则为 true;否则为 false。</returns>
public static bool EqualsSafely(this string @this, string value, StringComparison comparisonType)
{
if (@this == null)
{
return value == null;
}
return Enum.IsDefined(typeof(StringComparison), comparisonType) && @this.Equals(value, comparisonType);
}
示例7: PrintCompared
public static void PrintCompared (string a, string b, StringComparison comparison) {
var result = String.Compare(a, b, comparison);
if (result > 0)
result = 1;
else if (result < 0)
result = -1;
Console.WriteLine(result);
}
示例8: IsDefined
/// <summary>
/// Determines whether the specified <paramref name="value"/> is defined by the <see cref="StringComparison"/>
/// enumeration.
/// </summary>
/// <param name="value">The value to verify.</param>
/// <returns>
/// <c>true</c> if the specified options is defined; otherwise, <c>false</c>.
/// </returns>
public static bool IsDefined(StringComparison value)
{
return value == StringComparison.CurrentCulture ||
value == StringComparison.CurrentCultureIgnoreCase ||
value == StringComparison.InvariantCulture ||
value == StringComparison.InvariantCultureIgnoreCase ||
value == StringComparison.Ordinal ||
value == StringComparison.OrdinalIgnoreCase;
}
示例9: Contains
/// <summary>
/// Returns a value indicating whether the specified System.String object occurs within this string.
/// </summary>
/// <param name="input">The input string.</param>
/// <param name="value">The string to seek.</param>
/// <param name="comparisonType">One of the enumeration values that determines how this string and value are compared.</param>
/// <returns>Returns <c>true</c> if the <paramref name="value"/> is contained within this string; otherwise <c>false</c>.</returns>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="input"/> is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="value"/> is null</exception>
public static bool Contains(this string input, string value, StringComparison comparisonType)
{
if (input == null) {
throw new ArgumentNullException("input", "input cannot be null");
}
else if (value == null) {
throw new ArgumentNullException("value", "value cannot be null");
}
return input.IndexOf(value, comparisonType) > -1;
}
示例10: ContainsAll
/// <summary>
/// A string extension method that query if this object contains the given @this.
/// </summary>
/// <param name="this">The @this to act on.</param>
/// <param name="comparisonType">Type of the comparison.</param>
/// <param name="values">A variable-length parameters list containing values.</param>
/// <returns>true if it contains all values, otherwise false.</returns>
public static bool ContainsAll(this string @this, StringComparison comparisonType, params string[] values)
{
foreach (string value in values)
{
if (@this.IndexOf(value, comparisonType) == -1)
{
return false;
}
}
return true;
}
示例11: Contains
/// <summary>
/// 返回一个值,该值指示指定的 System.String 对象是否出现在此字符串中。
/// </summary>
/// <param name="value">当前 System.String 对象。</param>
/// <param name="comparisonValue">要搜寻的字符串。</param>
/// <param name="comparisonType">指定搜索规则的枚举值。</param>
/// <returns>如果 value 参数出现在此字符串中,或者 value 为空字符串 (""),则为 true;否则为 false。</returns>
/// <exception cref="System.ArgumentNullException"><c>value</c> 为 null。</exception>
public static bool Contains(this string value, string comparisonValue, StringComparison comparisonType)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if (value.Length == 0)
{
return true;
}
return value.IndexOf(comparisonValue, comparisonType) != -1;
}
示例12: FindUserWithName
/// <summary>
/// Look through a set of users for a user with a specific name
/// </summary>
/// <param name="siteUsers"></param>
/// <param name="findName"></param>
/// <param name="compareMode"></param>
/// <returns> NULL = No matching user found. Otherwise returns the user with the matching name
/// </returns>
public static SiteUser FindUserWithName(IEnumerable<SiteUser> siteUsers, string findName, StringComparison compareMode = StringComparison.InvariantCultureIgnoreCase)
{
foreach(var thisUser in siteUsers)
{
//If its a match return the user
if(string.Compare(thisUser.Name, findName, compareMode) == 0)
{
return thisUser;
}
}
return null; //no found
}
示例13: Replace
public static string Replace(this string originalString, string oldValue, string newValue, StringComparison comparisonType)
{
int startIndex = 0;
while (true)
{
startIndex = originalString.IndexOf(oldValue, startIndex, comparisonType);
if (startIndex == -1)
break;
originalString = originalString.Substring(0, startIndex) + newValue + originalString.Substring(startIndex + oldValue.Length);
startIndex += newValue.Length;
}
return originalString;
}
示例14: ReplaceString
public static string ReplaceString(this string str, string oldValue, string newValue, StringComparison comparison)
{
StringBuilder sb = new StringBuilder();
int previousIndex = 0;
int index = str.IndexOf(oldValue, comparison);
while (index != -1)
{
sb.Append(str.Substring(previousIndex, index - previousIndex));
sb.Append(newValue);
index += oldValue.Length;
previousIndex = index;
index = str.IndexOf(oldValue, index, comparison);
}
sb.Append(str.Substring(previousIndex));
return sb.ToString();
}
示例15: RowConfigReader
/// <summary>
/// Constructs a new RowConfigReader which reads from the given string.
/// <param name="buffer">The string to parse through.</param>
/// </summary>
public RowConfigReader(string buffer)
{
_buffer = buffer;
_comparisonKind = StringComparison.Ordinal;
_currentIndex = 0;
}