Solved

C#  Csharp findin a string within a string when you have a starting point to go from

Posted on 2011-03-19
7
343 Views
Last Modified: 2013-12-17
have a string that i am trying to find.
as in <br> which starts 72 to 90chars in for example an i need to start the search at say char 35 or may be 45 dependng on what was last found
am using idx = str.IndexOf("  string ")  but this does not allow me to start from so many characters in
any help would be helpful
especially looking for a good web site with all the code functions published with examples
0
Comment
Question by:sydneyguy
  • 4
  • 3
7 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35174174
IndexOf() is overloaded to take a 2nd parameter of starting index:
string s = "Hello World!";

int index = s.IndexOf("W", 2);

Open in new window

0
 

Author Comment

by:sydneyguy
ID: 35174186
thanks most appreciated.
do you know any sites with a really good range of c# functions and examples please
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35174237
No I do not, but a quick search returned the following:


Also, you should have Intellisense, if you are using Visual Studio, which will show you when a function is overloaded.

Intellisense
Here you can see that the IndexOf() method has 9 overloads and the 4th overload takes in a string to find and a starting index to begin searching from.

You can also use the Object Browser (View->Object Browser) to view the methods for classes. For example, here is a sampling of what is shown for the string class:

 String Class Members
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:sydneyguy
ID: 35174246
i have intelli sense running but when it come to a function that i need being
i know that my start point is 157 chars in
but what i need to be able to do is to serch going to the left to the beginning and find the first match substring.
so instead of starting at loc 57 and then search in from there to the end i need to start in the middle at some given loc and search back towards the beginnings and find the first match  some sort of
str.InsexOf("data"); but going in the oppoiste dir given a starting point
any ideas then i will close out the question
thanks for the help so far
0
 

Author Comment

by:sydneyguy
ID: 35174258
got it its str.lastIndexOf("data",100);
works perfect thanks for the help
0
 

Author Closing Comment

by:sydneyguy
ID: 35174261
thanks for the help
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 35174275
I think for what it sounds like you want to do, you would need a combination of two functions:

string s = "Hello World! Hello.";

int index = s.Substring(0, 7).LastIndexOf("ell");

Open in new window


Here, you would put your starting location where you see the 7. This will give you the substring from the first character to your expected starting position. You would then use LastIndexOf() to find the last occurrence of your search string within this "new" string.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now