xxxxxxxxxx
React Toast Plus is a good solution for you
install with npm
npm install react-toast-plus
Or with Yarn:
yarn add react-toast-plus
Basic Setup
Wrap your application with the ToastProvider to enable toast notifications:
import React from 'react';
import ReactDOM from 'react-dom';
import { ToastProvider } from 'react-toast-plus';
import App from './App';
ReactDOM.render(
<ToastProvider>
<App />
</ToastProvider>,
document.getElementById('root')
);
You can use the useToast hook to add toasts:
import React from 'react';
import { useToast } from 'react-toast-plus';
const App = () => {
const { addToast } = useToast();
const showToast = () => {
addToast('This is a toast!');
};
return <button onClick={showToast}>Show Toast</button>;
};
xxxxxxxxxx
<ToastProvider toastOptions={{
closeOnClick: true,
successOptions: {
style: { backgroundColor: 'green' },
icon: <cutomIcon/>
},
errorOptions: {
closeButton: { visible: false } ,
lifetime: 5000
},
}}>
<App />
</ToastProvider>
xxxxxxxxxx
React Toast Plus is a lightweight, customizable toast notification library for React.
Installation
To install react-toast-plus, run:
npm install react-toast-plus
Or with Yarn:
yarn add react-toast-plus
Basic Setup
Wrap your application with the ToastProvider to enable toast notifications:
import React from 'react';
import ReactDOM from 'react-dom';
import { ToastProvider } from 'react-toast-plus';
import App from './App';
ReactDOM.render(
<ToastProvider>
<App />
</ToastProvider>,
document.getElementById('root')
);
Adding Toasts
import React from 'react';
import { useToast } from 'react-toast-plus';
const App = () => {
const { addToast } = useToast();
const showToast = () => {
addToast('This is a toast!');
};
return <button onClick={showToast}>Show Toast</button>;
};
xxxxxxxxxx
string stringtofind = "search-string";
reallylongstring.Contains(stringtofind);
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
if (sb.ToString().Contains(stringtofind)) {
// stringtofind is found in sb
} else {
// stringtofind is not found in sb
}
xxxxxxxxxx
/* You can use the IndexOf method of the StringBuilder class to check whether a string
is contained within the StringBuilder object. Here's an example code snippet: */
StringBuilder sb = new StringBuilder(reallylongstring);
string stringtofind = "some string to find";
bool isFound = sb.ToString().IndexOf(stringtofind) != -1;
if (isFound)
{
// The string is found
}
else
{
// The string is not found
}
/* In this code, we create a StringBuilder object named sb with the reallylongstring
as the initial content. Then, we define a string variable named stringtofind
that contains the string we want to search for. We use the ToString method
of the StringBuilder class to convert the StringBuilder object into a string,
and then call the IndexOf method to search for the stringtofind variable within
the StringBuilder object. If the IndexOf method returns a value other than -1,
it means that the stringtofind variable is found in the StringBuilder object. */
xxxxxxxxxx
import React from 'react';
import { ToastContainer, toast } from 'material-react-toastify';
import 'material-react-toastify/dist/ReactToastify.css';
function App(){
const notify = () => toast("Wow so easy !");
return (
<div>
<button onClick={notify}>Notify !</button>
<ToastContainer />
</div>
);
}
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;
}
}