Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
How to find the first index of a sub-string in a given string
//Rextester.Program.Main is the entry point for your code. Don't change it. //Microsoft (R) Visual C# Compiler version 2.9.0.63208 (958f2354) using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //How to find the first index of a substring in a given string? //use question as given string and find the index of the substring "fir". //Builin array functions and LINQ are not allowed. string strMainString = "How to find the first index of a sub-string in a given string?" ; string srtSubstring = "fir"; int iMainstrLenght = strMainString.Length; int iSubstringLenght = srtSubstring.Length; bool bfound = false; for(int i=0 ;i<=iMainstrLenght-iSubstringLenght;i++) { int j; for(j=0;j<iSubstringLenght;j++) if(strMainString[i+j]!=srtSubstring[j]) break; if(j==iSubstringLenght) { bfound = true; Console.WriteLine("Index is "+i ); } } if(!bfound) Console.WriteLine("No substring found" ); } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
x=msgbox("Your Message Here", Button+Icon, "Your Title Here")
HTML Hex to RGB without using System.Drawing C#
D15
Ejercicio bucles
UrlDecode
Plt-D v.0.9.6 (Speed Vector)
Big numbers
Reverse String in for loop
12
math 5.74
Please log in to post a comment.