SearchBox/SearchBox/Utilities/StringPlus.cs

35 lines
860 B
C#

using System;
using System.Collections.Generic;
namespace LibSearchBox.Utilities
{
public static class StringPlus
{
public static string ReplaceMultiple(this string str, char[] find, char[] replace)
{
for (int i = 0; i < find.Length; i++) {
str = str.Replace(
find[i],
i < replace.Length ? replace[i] : replace[replace.Length - 1]
);
}
return str;
}
// From https://stackoverflow.com/a/2641383/1460422
public static IEnumerable<int> AllIndexesOf(this string str, string value)
{
if (String.IsNullOrEmpty(value))
throw new ArgumentException("Error: The string to find may not be empty.", nameof(value));
List<int> indexes = new List<int>();
for (int index = 0; ; index += value.Length) {
index = str.IndexOf(value, index);
if (index == -1)
break;
yield return index;
}
}
}
}