Split a String

  • Thread starter Thread starter Roshawn
  • Start date Start date
R

Roshawn

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
 
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
 
Thanks guys (and gals just to be polite) for your prompt responses. These
techniques are just what I was looking for.

Roshawn
 
Very nice. I didnt even think of those calls. Learn something new everyday.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top