Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variation on String.Split()

Status
Not open for further replies.

Meleagant

Programmer
Aug 31, 2001
166
0
0
US
All,

Is there an easy way where I can split a string and actually get the delimiter as part of the array? For example:
Code:
Dim myString as String = "Charges + Allowances - Bad Debt"

Dim parts as String() = myString.Split("/*-+".ToCharArray)

Parts would then yield:
parts(0) = "Charges"
parts(1) = "+"
parts(2) = "Allowances"
parts(3) = "-"
parts(4) = "Bad Debt"

* Sine scientia ars nihil est
* Respondeat superior
 

Well, almost what you want (split it by a Space)
Code:
Dim myString As String = "Charges + Allowances - Bad Debt"

Dim parts As String() = myString.Split(" ".ToCharArray)

For i As Integer = LBound(parts) To UBound(parts)
   Debug.Print(parts(i))
Next

[tt]Charges
+
Allowances
-
Bad
Debt[/tt]

Have fun.

---- Andy
 
I actually just got it. I found another post here that when using RegEx I could use (|-).

Using this as a starting point I was able to get what I needed. I'm not sure how this works but it does:

Code:
Dim myString As String = "Charges + Allowances - Bad Debt"
Dim parts As String() = Regex.Split(myString, "(\+|\-|\*|\/)")



* Sine scientia ars nihil est
* Respondeat superior
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top