Bryan,
|I have a string that is delimited, e.g.
| "SOD;NAME;ADDRESS1;ADDRESS2;PHONE;CITY;STATE;ZIP;E OD", that I need to
strip
| off the "SOD;" at the beginning and the ";EOD" at the end.
With "fixed" prefixes & suffixes I would use StartsWith & EndsWith coupled
with SubString.
Something like:
Private Function TrimContent(ByVal input As String, ByVal prefix As
String, ByVal suffix As String) As String
Dim startIndex As Integer = 0
Dim length As Integer = input.Length
If input.StartsWith(prefix) Then
startIndex += prefix.Length
length -= prefix.Length
End If
If input.EndsWith(suffix) Then
length -= suffix.Length
End If
Return input.Substring(startIndex, length)
End Function
Public Sub Main()
Dim input As String =
"SOD;NAME;ADDRESS1;ADDRESS2;PHONE;CITY;STATE;ZIP;E OD"
Dim output As String = TrimContent(input, "SOD;", ";EOD")
Debug.WriteLine(input, "input")
Debug.WriteLine(output, "output")
End Sub
The advantage of TrimContent is that it does not create any temporary string
objects, using Split & Join you would be creating a number of temporary
string objects that may be putting undue pressure on the GC.
The StartsWith & EndsWith are there more to ensure that valid data is not
trimmed accidentally.
You would need to profile whether StartsWith/EndWith, IndexOf, or Split/Join
would be faster, however I would find StartsWith/EndsWith or IndexOf to be
the faster routines, plus more importantly more obvious what you are
attempting to do.
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Bryan Dickerson" <tx******@netscape.net> wrote in message
news:O$*************@tk2msftngp13.phx.gbl...
|I have a string that is delimited, e.g.
| "SOD;NAME;ADDRESS1;ADDRESS2;PHONE;CITY;STATE;ZIP;E OD", that I need to
strip
| off the "SOD;" at the beginning and the ";EOD" at the end. I am well
| familiar with the Split and Join functions that can split a string like
this
| out to an array and vice versa, but is there a way that I can split the
| string into an array and then join it back to a string along with telling
it
| to ignore the first and last element? Or even some way to ReDim the
"split"
| array to ignore certain elements? I know I can do it the old fashioned
way
| with .IndexOf methods, but I was thinking that the Split and Join
functions
| would be somewhat faster. Would a stringbuilder array and a loop be much
| faster yet (these strings can get to be pretty good size--thousands of
| bytes)? Just thinking "out loud" as I wrote.
|
| Thanx in advance for your thoughts!
|
| --
| TFWBWY...A
|
|