Solved

VB.net substring help

Posted on 2012-03-20
8
239 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
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!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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