Solved

VB.net substring help

Posted on 2012-03-20
8
243 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:Mike Miller
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Mike Miller
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:Mike Miller
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

728 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