By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,217 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,217 IT Pros & Developers. It's quick & easy.

Split a String

P: n/a
Hi,

I am retrieving a list of book titles from a web service. What I'd like to
do is shorten the titles, if possible. For example, there is a book titled
"Malicious Mobile Code: Virus Protection for Windows". It would be nice if
I could trim the title down to the colon (it would just say Malicious Mobile
Code).

Is there an easy way to accomplish this task?

Thanks,
Roshawn
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Maybe you could use the instr() function to return the first occurence of the
colon, and then use the left or mid function to return the left side of the
name?

mid:
http://msdn.microsoft.com/library/de...l/vafctMid.asp
left:
http://msdn.microsoft.com/library/de.../vafctleft.asp

instr:
http://msdn.microsoft.com/library/de...vafctinstr.asp

Does this help you?

"Roshawn" wrote:
Hi,

I am retrieving a list of book titles from a web service. What I'd like to
do is shorten the titles, if possible. For example, there is a book titled
"Malicious Mobile Code: Virus Protection for Windows". It would be nice if
I could trim the title down to the colon (it would just say Malicious Mobile
Code).

Is there an easy way to accomplish this task?

Thanks,
Roshawn

Nov 21 '05 #2

P: n/a
Roshawn,
In addition to the VB runtime functions r_burgess suggested.

You can use String.Split to split the name.

Dim title As string = "Malicious Mobile Code: Virus Protection for
Windows"
Dim parts() As String = title.Split(":"c)

The parts(0) will contain "Malicious Mobile Code", while parts(1) will
contain " Virus Protection for Windows"

Alternatively you could use String.IndexOf & String.SubString along with
String.Trim to remove leading & trailing white space.

Dim title As string = "Malicious Mobile Code: Virus Protection for
Windows"
Dim index As Integer = title.IndexOf(":"c)
Dim parts(1) As String
parts(0) = title.Substring(0, index).Trim()
parts(1) = title.Substring(index + 1).Trim()

Note you can use String.LastIndexOf if you want to split the name on the
last ":" instead of the first ":" if there are multiple ":" in the name.

I normally avoid mixing the VB runtime functions (InStr, Left, Right, Mid)
with the String methods (String.IndexOf, String.SubString) within the same
function/class/project/solution as the runtime functions are 1 based
indexes, while the String methods are 0 based indexes.

Hope this helps
Jay

"Roshawn" <ud****@bellsouth.net> wrote in message
news:ed**************@TK2MSFTNGP15.phx.gbl...
Hi,

I am retrieving a list of book titles from a web service. What I'd like
to
do is shorten the titles, if possible. For example, there is a book
titled
"Malicious Mobile Code: Virus Protection for Windows". It would be nice
if
I could trim the title down to the colon (it would just say Malicious
Mobile
Code).

Is there an easy way to accomplish this task?

Thanks,
Roshawn

Nov 21 '05 #3

P: n/a
Thanks guys (and gals just to be polite) for your prompt responses. These
techniques are just what I was looking for.

Roshawn
Nov 21 '05 #4

P: n/a
Very nice. I didnt even think of those calls. Learn something new everyday.

"Jay B. Harlow [MVP - Outlook]" wrote:
Roshawn,
In addition to the VB runtime functions r_burgess suggested.

You can use String.Split to split the name.

Dim title As string = "Malicious Mobile Code: Virus Protection for
Windows"
Dim parts() As String = title.Split(":"c)

The parts(0) will contain "Malicious Mobile Code", while parts(1) will
contain " Virus Protection for Windows"

Alternatively you could use String.IndexOf & String.SubString along with
String.Trim to remove leading & trailing white space.

Dim title As string = "Malicious Mobile Code: Virus Protection for
Windows"
Dim index As Integer = title.IndexOf(":"c)
Dim parts(1) As String
parts(0) = title.Substring(0, index).Trim()
parts(1) = title.Substring(index + 1).Trim()

Note you can use String.LastIndexOf if you want to split the name on the
last ":" instead of the first ":" if there are multiple ":" in the name.

I normally avoid mixing the VB runtime functions (InStr, Left, Right, Mid)
with the String methods (String.IndexOf, String.SubString) within the same
function/class/project/solution as the runtime functions are 1 based
indexes, while the String methods are 0 based indexes.

Hope this helps
Jay

"Roshawn" <ud****@bellsouth.net> wrote in message
news:ed**************@TK2MSFTNGP15.phx.gbl...
Hi,

I am retrieving a list of book titles from a web service. What I'd like
to
do is shorten the titles, if possible. For example, there is a book
titled
"Malicious Mobile Code: Virus Protection for Windows". It would be nice
if
I could trim the title down to the colon (it would just say Malicious
Mobile
Code).

Is there an easy way to accomplish this task?

Thanks,
Roshawn


Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.