xxxxxxxxxx
You can use the IndexOf method of the StringBuilder class to determine if the string stored in the stringtofind object is contained within the sb object. Here's an example of how you could do it:
if (sb.ToString().IndexOf(stringtofind) != -1)
{
// the string was found in the StringBuilder object
}
else
{
// the string was not found in the StringBuilder object
}
Note that the ToString() method is called on the StringBuilder object to convert it to a regular string before calling IndexOf.
xxxxxxxxxx
string stringtofind = "search-string";
reallylongstring.Contains(stringtofind);
xxxxxxxxxx
if (sb.ToString().Contains(stringtofind)) {
// stringtofind is found in sb
} else {
// stringtofind is not found in sb
}
xxxxxxxxxx
/** There are a number of ways to accomplish that
1) Use the default StringBuilder function `IndexOf`
2) Another way is to implement a fast search algorithm `Knuth-Morris-Pratt (KMP) algorithm,
which is an efficient string searching algorithm that works by precomputing a partial
match table based on the pattern to be searched.
The KMP algorithm has a time complexity of O(n+m), where n is the length of the text
to be searched and m is the length of the pattern to be searched.
.
.
.
Here's an example implementation of the KMP algorithm in C#:
**/
public static int KMP(string text, string pattern)
{
int n = text.Length;
int m = pattern.Length;
int[] pi = ComputePrefix(pattern);
int j = 0;
for (int i = 0; i < n; i++)
{
while (j > 0 && pattern[j] != text[i])
{
j = pi[j - 1];
}
if (pattern[j] == text[i])
{
j++;
}
if (j == m)
{
return i - m + 1;
}
}
return -1;
}
private static int[] ComputePrefix(string pattern)
{
int m = pattern.Length;
int[] pi = new int[m];
int j = 0;
for (int i = 1; i < m; i++)
{
while (j > 0 && pattern[j] != pattern[i])
{
j = pi[j - 1];
}
if (pattern[j] == pattern[i])
{
j++;
}
pi[i] = j;
}
return pi;
}
// To use the above code to search for a string within a StringBuilder object,
// you can simply call the KMP method with the ToString() method of the
// StringBuilder object and the string you want to search for:
StringBuilder sb = new StringBuilder(reallylongstring);
string stringtofind = "some string";
int index = KMP(sb.ToString(), stringtofind);
if (index != -1)
{
Console.WriteLine("The string '{0}' is found at index {1}.", stringtofind, index);
}
else
{
Console.WriteLine("The string '{0}' is not found.", stringtofind);
}
// Note:
// that the ToString() method is called to convert the StringBuilder
// object to a string before passing it to the KMP method.
// https://www.geeksforgeeks.org/kmp-algorithm-for-pattern-searching/
xxxxxxxxxx
public static class StringBuilderSearching
{
public static bool Contains(this StringBuilder haystack, string needle)
{
return haystack.IndexOf(needle) != -1;
}
public static int IndexOf(this StringBuilder haystack, string needle)
{
if(haystack == null || needle == null)
throw new ArgumentNullException();
if(needle.Length == 0)
return 0;//empty strings are everywhere!
if(needle.Length == 1)//can't beat just spinning through for it
{
char c = needle[0];
for(int idx = 0; idx != haystack.Length; ++idx)
if(haystack[idx] == c)
return idx;
return -1;
}
int m = 0;
int i = 0;
int[] T = KMPTable(needle);
while(m + i < haystack.Length)
{
if(needle[i] == haystack[m + i])
{
if(i == needle.Length - 1)
return m == needle.Length ? -1 : m;//match -1 = failure to find conventional in .NET
++i;
}
else
{
m = m + i - T[i];
i = T[i] > -1 ? T[i] : 0;
}
}
return -1;
}
private static int[] KMPTable(string sought)
{
int[] table = new int[sought.Length];
int pos = 2;
int cnd = 0;
table[0] = -1;
table[1] = 0;
while(pos < table.Length)
if(sought[pos - 1] == sought[cnd])
table[pos++] = ++cnd;
else if(cnd > 0)
cnd = table[cnd];
else
table[pos++] = 0;
return table;
}
}
xxxxxxxxxx
You can use the StringBuilder.IndexOf() method to check if the stringtofind is contained within the StringBuilder object. Here is an example of how you can use it:if (sb.IndexOf(stringtofind) >= 0)
{
// stringtofind was found within sb
}
else
{
// stringtofind was not found within sb
}
Alternatively, you can also use the StringBuilder.ToString() method to convert the StringBuilder object to a string and use the String.Contains() method to check if the stringtofind is contained within the string. Here is an example of how you can use it:
xxxxxxxxxx
// You can use the StringBuilder.IndexOf(string) method to find the index of the first occurrence of the stringtofind string within the sb StringBuilder object.
//
// Here is an example of how to use this method:
int index = sb.IndexOf(stringtofind);
if (index >= 0)
{
// stringtofind was found within sb
}
else
{
// stringtofind was not found within sb
}
xxxxxxxxxx
if (sb.IndexOf(stringtofind) >= 0)
{
// stringtofind was found in sb
}
else
{
// stringtofind was not found in sb
}
//OR
if (sb.Contains(stringtofind))
{
// stringtofind was found in sb
}
else
{
// stringtofind was not found in sb
}
xxxxxxxxxx
There are a few different ways to check if a string stored in an object named "stringtofind" is within the StringBuilder "sb" object in C#, but one common way is to use the IndexOf method:
if (sb.ToString().IndexOf(stringtofind) != -1)
{
// The stringtofind was found within the sb object
}
Another way is using Contains method
if (sb.ToString().Contains(stringtofind))
{
// The stringtofind was found within the sb object
}
Both methods works the same but the way they indicate if the string is found is different.
The IndexOf method will return -1 if the string is not found, otherwise it returns the index of the first occurrence of the specified string. The Contains method returns a Boolean indicating whether the specified string occurs within this object.
In both cases, you're calling the ToString() method on the sb object, which converts it to a regular string so you can use the IndexOf or Contains method on it.
It's important to keep in mind that the IndexOf and Contains method are case-sensitive, if you want to check the string with ignoring the case, you can use IndexOf or Contains method along with String.Compare method with 'StringComparison.OrdinalIgnoreCase' option.
xxxxxxxxxx
You can use the StringBuilder.IndexOf(string) method to check if the stringtofind is within the StringBuilder object sb.
Here's an example:
string stringtofind = "findme";
int index = sb.ToString().IndexOf(stringtofind);
if (index >= 0) {
Console.WriteLine("The string '{0}' was found in the StringBuilder at index {1}.", stringtofind, index);
} else {
Console.WriteLine("The string '{0}' was not found in the StringBuilder.", stringtofind);
}
You can call the .ToString() Method of the stringbuilder to get the value of the stringbuilder to perform the search.