|
|
#1 |
|
New Member
Join Date: Jun 07
Posts: 10
![]() |
[Resolved] Sub string within a string
Hi
New to VB, but not to programming. I need to search lines of text for substrings. I recon LastIndexOfAny is the way to go. As an example, if I wanted to find the last occurance of the word "for" in: "This string is just for tests purposes and should not be taken for any other reasons." When I have tried various ways I get it returning an index of one of the letters of 'for' or at least that is what it looks like. I would be looking for a reply of 63 for the string above (presuming I counted corectly) as it's the last occurence of for. Not a return indicating the last occurance of 'o' instead or some other reply. Code:
current_line = "This string is just for tests purposes and should not be used for any other reasons."
count2 = current_line.LastIndexOfAny("for")
Last edited by Writers Block; Jun 17th, 2007 at 05:43 PM. Reason: [Resolved] |
|
|
|
|
|
#2 |
|
Frenzied Member
Join Date: Jun 05
Location: South UK
Posts: 1,653
![]() ![]() ![]() ![]() |
Re: Sub string within a string
I get the result of 63 using "LastIndexOf" (the first character position is zero).
Code:
Dim s As String = "This string is just for tests purposes and should not be taken for any other reasons."
MessageBox.Show(s.LastIndexOf("for"))
__________________
_________________________________________________________________________________ B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA I wrote my very first program in 1975, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved. |
|
|
|
|
|
#3 |
|
New Member
Join Date: Jun 07
Posts: 10
![]() |
Re: Sub string within a string
I get 80. As though it is returning the postion of a character in another word, not the whole word. And it is the whole word I am looking for.
|
|
|
|
|
|
#4 |
|
New Member
Join Date: Jun 07
Posts: 10
![]() |
Re: Sub string within a string
No matter i feel stupid. Got it now.
|
|
|
|
![]() |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|