473,394 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Working with strings and arrays

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
Dec 2 '05 #1
6 1412
Bryan Dickerson wrote:
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!

If you just want to find the first and last items then split and join
will not be fast by any means. Using split it goes through each
character in your array, finds the index item copies each item to an
array. if you just do a String.Indexof and String.LastIndexof and then
do a substring, the number of comparisons and the number of copies that
have to take place are set at 2 each. that's the way you should do this.

Chris
Dec 2 '05 #2
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
|
|
Dec 2 '05 #3
Thanx! After I posted this, I thought, "I can just do some playing myself,"
but I figured the post would do well at seeing if there were any new ways
that you could do this. So I had fairly settled on a method very similar to
this after trying to use Split and re-Join or "String-Build" it back
together & seeing that I didn't gain much speed. I guess ultimately old
methods are not necessarily to be thrown out, I just need to be smart in how
& where I implement them. It's interesting, though, 'cause in VB6 I had
some routines that used simple substringing ("Mid$" functions, etc) to break
apart long strings and when I changed them to use Split, Join and dynamic
arrays, they were measurably speeded up. Any ideas as to why? And any
ideas as to why they don't bring the same measure of speed in .Net?
"Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> wrote in
message news:%2****************@TK2MSFTNGP15.phx.gbl...
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
|
|

Dec 2 '05 #4
For the sake of conversation if you want to solve this with split/join,
here's one way you could do it:

Dim wholeArray() as string
Dim trimmedArray() as string

wholeArray = myString.Split(";")
Array.Copy(wholeArray, 1, trimmedArray, wholeArray.Length - 2)
myString = trimmedArray.Join(";")

But if you mean to say that "SOD;" is literally at the beginning of the
string and ";EOD" is literally at the end, and you wish to obtain a new
string without them, this would probably be both easier and faster:

myString = myString.Substr(4, myString.Length - 8)

"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; EOD", 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

Dec 3 '05 #5
Bryan,

If I understand you well, than it looks very easy to me.

Find the startindex from ";" with indexof
Find the lastIndex from ";" with LastIndexof
Create a newstring using myOldString.Substring(startindex+1, lastindexof +
1 - startindex)

I hope this helps,

Cor

Dec 3 '05 #6
Thanx Cor. You and everyone else have been very helpful on this.

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:eq***************@TK2MSFTNGP11.phx.gbl...
Bryan,

If I understand you well, than it looks very easy to me.

Find the startindex from ";" with indexof
Find the lastIndex from ";" with LastIndexof
Create a newstring using myOldString.Substring(startindex+1, lastindexof +
1 - startindex)

I hope this helps,

Cor

Dec 5 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
5
by: Robert | last post by:
Hi, Is there some way of using an array of strings? Like in basic? I know you have to create an array of chars so i think it has to be an 2d array or something... Really stuck here... Thanks...
10
by: Ian Todd | last post by:
Hi, I am trying to read in a list of data from a file. Each line has a string in its first column. This is what i want to read. I could start by saying char to read in 1000 lines to the array( i...
2
by: Steve | last post by:
I want an initializer for an array of pointers to arrays of strings. So I can do something like this: const char *t1 = { "a", "b", "c", NULL }; const char *t2 = { "p", "q", NULL }; const char...
7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
2
by: Noozer | last post by:
Hi! Trying to get my head around working with network connectivity in VB.Net. I've gotten a fairly decent control based on the sample in the 101VBSamples available from Microsoft.What I'm having...
6
by: Broeisi | last post by:
Hello, I wrote the tiny progam below just to understand arrays and strings better. I have 2 questions about arrays and strings in C. 1. Why is it that when you want to assign a string to an...
23
by: arnuld | last post by:
i was doing exercise 4.3.1 - 4.29 of "C++ Primer 4/e" where authors, with "run-time shown", claim that C++ Library strings are faster than C-style character strings. i wrote the same programme in...
19
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.