-
Apr 11th, 2019, 05:48 AM
#1
Thread Starter
New Member
/VB Express 2010/ Extract specify text from string
hello all
I'm new in visual basic and need little help...
i need to extract the revision of document from doc path.>
dim path as string = "\test\project\documents\reports\Result_001.txt"
so the revision is between "_" and "." in this case 001
and i need to extract it to another string.
how to do it?
-
Apr 11th, 2019, 07:46 AM
#2
Re: /VB Express 2010/ Extract specify text from string
This would be prefect for RegEx but I think that may be a bit confusing for a newbie so I'll show the classic way.
Code:
Dim path As String = "\test\project\documents\reports\Result_001.txt"
'note that IndexOf is 0-based
Dim start As Integer = path.IndexOf("_") 'find first occurrence of _
If start >= 0 Then
Dim endpos As Integer = path.IndexOf(".", start) 'find first occurrence of . after the _
If endpos >= 0 Then
'add 1 to start to ignore the _ but will need to subtract 1 to get correct length
MessageBox.Show("Revision = " & path.Substring(start + 1, endpos - start - 1))
Else
MessageBox.Show(". not found in path")
End If
Else
MessageBox.Show("_ not found in path")
End If
-
Apr 13th, 2019, 08:09 AM
#3
Re: /VB Express 2010/ Extract specify text from string
another way...
VB.Net Code:
dim path as string = "\test\project\documents\reports\Result_001.txt" dim fileName as string = IO.Path.GetFileNameWithOutExtension(path) dim parts() as string = fileName.Split("_"c) if(parts.Count = 2) MessageBox.Show(String.Format("The revision is {0}",parts(1)) endif
edit,
or if you don't want to use the String.Split method...
VB.Net Code:
dim path as string = "\test\project\documents\reports\Result_001.txt" dim fileName as string = IO.Path.GetFileNameWithOutExtension(path) if(fileName.Contains("_") dim revision as string= fileName.subString(fileName.LastIndexOf("_")) MessageBox.Show(String.Format("The revision is {0}",revision)) endif
Freehand code, but either should be close.
Last edited by kebo; Apr 13th, 2019 at 09:03 AM.
Process control doesn't give you good quality, it gives you consistent quality.
Good quality comes from consistently doing the right things.
Vague general questions have vague general answers. A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.
______________________________ Last edited by kebo : Now. Reason: superfluous typo's
-
Apr 17th, 2019, 09:15 AM
#4
Thread Starter
New Member
Re: /VB Express 2010/ Extract specify text from string
Perfect. thanks
-
Apr 17th, 2019, 09:32 AM
#5
Re: /VB Express 2010/ Extract specify text from string
FYI the regex that topshot mentioned is:
VB.Net Code:
Dim path = "\test\project\documents\reports\Result_001.txt" Dim revision = System.Text.RegularExpressions.Regex.Match(path, "(?<=_).*?(?=\.)").Value
Regex breakdown:
(?<=_) - Where the bit before is a _
.*? - Match one or more of any character until
(?=\.) - the following character is a .
Kris
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|