Solved

VB.net substring help

Posted on 2012-03-20
8
235 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…

828 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