Solved

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

Posted on 2011-03-19
7
352 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 75

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 75

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 75

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
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 and create new code templates in NetBeans IDE 8.0 for Windows.

805 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