本文整理汇总了C#中StringComparison类的典型用法代码示例。如果您正苦于以下问题:C# StringComparison类的具体用法?C# StringComparison怎么用?C# StringComparison使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StringComparison类属于命名空间,在下文中一共展示了StringComparison类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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();
}
开发者ID:tathamoddie,项目名称:System.IO.Abstractions,代码行数:32,代码来源:StringExtensions.cs
示例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);
}
}
开发者ID:h82258652,项目名称:StringExtension,代码行数:40,代码来源:StringExtension.Replace.cs
示例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));
}
}
开发者ID:Vizzini,项目名称:aspnetwebstack,代码行数:13,代码来源:StringComparisonHelper.cs
示例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;
}
开发者ID:jonocairns,项目名称:csharp-utils,代码行数:10,代码来源:StringExtensions.cs
示例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);
}
}
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:13,代码来源:StringComparisonHelper.cs
示例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);
}
开发者ID:h82258652,项目名称:StringExtension,代码行数:15,代码来源:StringExtension.Equals.cs
示例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);
}
开发者ID:cbsistem,项目名称:JSIL,代码行数:8,代码来源:StringCompare.cs
示例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;
}
开发者ID:haoduotnt,项目名称:aspnetwebstack,代码行数:17,代码来源:StringComparisonHelper.cs
示例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;
}
开发者ID:tjb042,项目名称:Archipelago,代码行数:20,代码来源:ArchipelagoStringExtensions.cs
示例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;
}
开发者ID:fqybzhangji,项目名称:Z.ExtensionMethods,代码行数:18,代码来源:String.ContainsAll.cs
示例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;
}
开发者ID:h82258652,项目名称:StringExtension,代码行数:20,代码来源:StringExtension.Contains.cs
示例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
}
开发者ID:tableau,项目名称:TabMigrate,代码行数:21,代码来源:SiteUser_statics.cs
示例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;
}
开发者ID:DevilboxGames,项目名称:Flummery,代码行数:16,代码来源:ExtensionMethods.cs
示例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();
}
开发者ID:Magi1053,项目名称:ProCraft,代码行数:19,代码来源:Replace.cs
示例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;
}
开发者ID:ChuangYang,项目名称:corefx,代码行数:10,代码来源:RowConfigReader.cs
示例16: TestIndexOf_AllSubstrings
public static void TestIndexOf_AllSubstrings(string source, string substring, int i, StringComparison comparison)
{
bool ignoringCase =
comparison == StringComparison.OrdinalIgnoreCase ||
comparison == StringComparison.CurrentCultureIgnoreCase;
// First find the substring. We should be able to with all comparison types.
Assert.Equal(i, source.IndexOf(substring, comparison)); // in the whole string
Assert.Equal(i, source.IndexOf(substring, i, comparison)); // starting at substring
if (i > 0)
{
Assert.Equal(i, source.IndexOf(substring, i - 1, comparison)); // starting just before substring
}
Assert.Equal(-1, source.IndexOf(substring, i + 1, comparison)); // starting just after start of substring
// Shouldn't be able to find the substring if the count is less than substring's length
Assert.Equal(-1, source.IndexOf(substring, 0, substring.Length - 1, comparison));
// Now double the source. Make sure we find the first copy of the substring.
int halfLen = source.Length;
source += source;
Assert.Equal(i, source.IndexOf(substring, comparison));
// Now change the case of a letter.
source = source.ToUpperInvariant();
Assert.Equal(
ignoringCase ? i : -1,
source.IndexOf(substring, comparison));
}
开发者ID:hyjiacan,项目名称:corefx,代码行数:29,代码来源:String.cs
示例17: IsNameAlreadyAMemberName
internal static bool IsNameAlreadyAMemberName(StructuralType type, string generatedPropertyName, StringComparison comparison)
{
foreach (EdmMember member in type.Members)
{
if (member.DeclaringType == type &&
member.Name.Equals(generatedPropertyName, comparison))
{
return true;
}
}
return false;
}
开发者ID:nlh774,项目名称:DotNetReferenceSource,代码行数:13,代码来源:NavigationPropertyEmitter.cs
示例18: TestEndsWith
public static void TestEndsWith(StringComparison comparisonType, string text, string value, bool expected)
{
if (comparisonType == StringComparison.CurrentCulture)
{
Assert.Equal(expected, text.EndsWith(value));
}
Assert.Equal(expected, text.EndsWith(value, comparisonType));
}
开发者ID:hyjiacan,项目名称:corefx,代码行数:8,代码来源:String.cs
示例19: TestEndsWith_NullInStrings
public static void TestEndsWith_NullInStrings(StringComparison comparison)
{
Assert.True("\0test".EndsWith("test", comparison));
Assert.True("te\0st".EndsWith("e\0st", comparison));
Assert.False("te\0st".EndsWith("test", comparison));
Assert.False("test\0".EndsWith("test", comparison));
Assert.False("test".EndsWith("\0st", comparison));
}
开发者ID:hyjiacan,项目名称:corefx,代码行数:8,代码来源:String.cs
示例20: TestEquals
[InlineData("123", 123, StringComparison.OrdinalIgnoreCase, false)] // Not a string
public static void TestEquals(string s1, object obj, StringComparison comparisonType, bool expected)
{
string s2 = obj as string;
if (s1 != null)
{
if (comparisonType == StringComparison.Ordinal)
{
// Use Equals(object)
Assert.Equal(expected, s1.Equals(obj));
Assert.Equal(expected, s1.Equals(s2));
}
// Use Equals(string, comparisonType)
Assert.Equal(expected, s1.Equals(s2, comparisonType));
}
if (comparisonType == StringComparison.Ordinal)
{
// Use Equals(string, string)
Assert.Equal(expected, string.Equals(s1, s2));
}
// Use Equals(string, string, StringComparisonType)
Assert.Equal(expected, string.Equals(s1, s2, comparisonType));
// If two strings are equal ordinally, then they must have the same hash code.
if (s1 != null && s2 != null && comparisonType == StringComparison.Ordinal)
{
Assert.Equal(expected, s1.GetHashCode().Equals(s2.GetHashCode()));
}
if (s1 != null)
{
Assert.Equal(s1.GetHashCode(), s1.GetHashCode());
}
}
开发者ID:eerhardt,项目名称:corefx,代码行数:33,代码来源:String.cs
注:本文中的StringComparison类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论