Introduction : In this tutorial we will see how to Find string, word or character in a given string using C# and VB.NET
IndexOf() method is used to find if the word, or string contains in the given string or not. However you can use the Contains() mthod too for the same purpose.
C# Example code sampleusing System;
namespace DotNetSpark.Tutorials
{
class DotNetSpark
{
static void Main(string[] args)
{
// Declare string
string strValue = "DotNetSpark";
// Declare the another string and assign the word need to find
string strFind = "Dot";
// find the string using Indexof Method if it found it will return 0 else -1 in case of string is not found
int iSuccess = strValue.IndexOf(strFind);
// Display result
Console.WriteLine(iSuccess);
Console.ReadLine();
}
}
}
Output:
0
VB.NET code sampleNamespace DotNetSpark.Tutorials
Class DotNetSpark
Private Shared Sub Main(args As String())
' Declare string
Dim strValue As String = "DotNetSpark"
' Declare the another string and assign the word need to find
Dim strFind As String = "Dot"
' find the string using Indexof Method if it found it will return 0 else -1 in case of string is not found
Dim iSuccess As Integer = strValue.IndexOf(strFind)
' Display result
Console.WriteLine(iSuccess)
Console.ReadLine()
End Sub
End Class
End Namespace
Output:
0 |