Solved

VB.net substring help

Posted on 2012-03-20
8
226 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 39

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 74

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 39

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

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…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

22 Experts available now in Live!

Get 1:1 Help Now