Solved

VB.net substring help

Posted on 2012-03-20
8
229 Views
Last Modified: 2012-03-20
I'm trying to extract text..

If the first 12 characters are 5 numbers followed by a comma followed by 5 numbers followed by a comma, I want to assign everything following the second comma to a vaiable

i.e.
00582,00087,blahblah

myString would = blahblah

TIA
0
Comment
Question by:mwmiller78
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37743839
dim OrigString as string = "00582,00087,blahblah"
dim myString as String

myString =  OrigString.Split(",").Last
0
 

Author Comment

by:mwmiller78
ID: 37743848
Probably would work for me but what if the string is...


impor,tant,info,41341231

???
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 37743854
Assuming that the string format is fixed:
Dim myString As String = teststring.Substring(12)

Open in new window


Otherwise you could use code similar to what I answered in the last question about getting the content to the right of a particular character.
0
 

Author Comment

by:mwmiller78
ID: 37743862
It would only be fixed if there is the format of 5number comma 5 number comma

Could be

blah,blah,blah

in which case I would not want to change the string
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 37743885
Instead you might want to use a regular expression:

 Dim myString As String = System.Text.RegularExpressions.Regex.Replace(teststring, "^\d{5},\d{5},", "")

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37743888
Try

Dim idx As Integer = str.indexof(",")
if idx = 5 Then
   if str.Indexof(",", idx)=11 Then
      'further checks here and substring
   End If
End If
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37743893
Regex is probably overkill for this, but you could do:

Dim myString As String
Dim m As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(input, "^\d{5},\d{5},(.*)")

If m.Success Then
    myString = m.Groups(1).Value
Else
    myString = String.Empty
End If

Open in new window

0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 37743917
from:

dim OrigString as string = "00582,00087,blahblah"
dim myString as String = SubString(NthIndexOf(OrigString, "," , 2))




Public NotInheritable Class StringExtender
	Private Sub New()
	End Sub
	<System.Runtime.CompilerServices.Extension> _
	Public Shared Function NthIndexOf(target As String, value As String, n As Integer) As Integer
		Dim m As Match = Regex.Match(target, "((" & value & ").*?){" & n & "}")
		If m.Success Then
			Return m.Groups(2).Captures(n - 1).Index
		Else
			Return -1
		End If
	End Function
End Class

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

23 Experts available now in Live!

Get 1:1 Help Now