To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)
Introducing Visual Studio LightSwitch
Visual Studio LightSwitch Beta 1 is Available



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Display Modes
Old Jun 17th, 2007, 05:17 PM   #1
Writers Block
New Member
 
Join Date: Jun 07
Posts: 10
Writers Block is an unknown quantity at this point (<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]
Writers Block is offline   Reply With Quote
Old Jun 17th, 2007, 05:23 PM   #2
Bulldog
Frenzied Member
 
Bulldog's Avatar
 
Join Date: Jun 05
Location: South UK
Posts: 1,653
Bulldog is a jewel in the rough (300+)Bulldog is a jewel in the rough (300+)Bulldog is a jewel in the rough (300+)Bulldog is a jewel in the rough (300+)
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"))
__________________

  • If my post helped you, please Rate it
  • If your problem is solved please also mark the thread resolved
I use VS2005/8 Standard (unless otherwise stated).
_________________________________________________________________________________
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.
Bulldog is offline   Reply With Quote
Old Jun 17th, 2007, 05:37 PM   #3
Writers Block
New Member
 
Join Date: Jun 07
Posts: 10
Writers Block is an unknown quantity at this point (<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.
Writers Block is offline   Reply With Quote
Old Jun 17th, 2007, 05:40 PM   #4
Writers Block
New Member
 
Join Date: Jun 07
Posts: 10
Writers Block is an unknown quantity at this point (<10)
Re: Sub string within a string

No matter i feel stupid. Got it now.
Writers Block is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:07 AM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.