Mehdi
Mehdi

Reputation: 1031

How to find the index of the first char of a substring of string?

I have string like "Helloworld" and a substring of that like "world" now , how can I find the index of the first char of the substring in the main string ? in this case I want the index of 'w' in "Helloworld"

can anyone help me please ?

Upvotes: 1

Views: 125

Answers (3)

Steve
Steve

Reputation: 216293

I suggest to use the string.IndexOf, but specifically the overload that accepts the StringComparison enum

string test = "HelloWorld";  // World in uppercase 
int pos = test.IndexOf("world", StringComparison.CurrentCultureIgnoreCase);
Console.WriteLine(pos.ToString());

Upvotes: 4

Bogdan
Bogdan

Reputation: 484

"Helloworld".IndexOf("world");

Upvotes: 1

L.B
L.B

Reputation: 116118

var index = "Helloworld".IndexOf("world");  

will return 5

Upvotes: 2

Related Questions