tags:

views:

58

answers:

3

I'm trying to create a number of strings based on one long string that i'm passing.

Basically this is an example of my long string

StrMain = AL123456 - PR123456 - RD123456 - LO123456

So in this case I want to create 4 separate strings.

Str1 = AL123456
Str2 = PR123456
Str3 = RD123456
Str4 = LO123456

But there isn't always that many or there may be more so I need to count how many - there are and with that then create the amount strings needed.

Any ideas?

Thanks

Jamie

+2  A: 

You could use the Split function:

Dim tokens As String() = "AL123456 - PR123456 - RD123456 - LO123456".Split("-"C)

or if you want to use a string as separator:

Dim tokens As String() = "AL123456 - PR123456 - RD123456 - LO123456".Split({" - "}, StringSplitOptions.RemoveEmptyEntries)
Darin Dimitrov
Might want to split on `" - "` instead, since there are spaces in the string as well.
Bevin
@Bevin, did you click on the link I provided? Apparently not.
Darin Dimitrov
All im getting is `System.String[]` on my page when i use your code?
Jamie Taylor
That's because the Split function returns an array. You need to loop through the elements of this array and format the output depending on your needs.
Darin Dimitrov
In my `For Each` how can I create a string based on the output. I just want to create a label with the data from that string in and then display it on the page
Jamie Taylor
+2  A: 

Let we have:

var input = "AL123456 - PR123456 - RD123456 - LO123456"

then

input.Split('-');

will return

{ "AL123456 ", " PR123456 ", " RD123456 ", " LO123456" }

i.e. with leading and trailing spaces.

So you need trim each:

using System.Collections.Generic;
using System.Linq;

IEnumerable<string> result = input.Split('-').Select(s => s.Trim());

(Select() requires .NET 3.5+)


Or just split by " - ":

var result = input.Split(new string[] { " - " }, StringSplitOptions.None);

or using VB.NET syntax:

Dim result As String() = input.Split({ " - " }, StringSplitOptions.None)

I guess VB.NET has next syntax for for-each:

For Each str As String In result
    Response.Write(str) ' or use str in other context '
End For
abatishchev
surely this creates one string? I need four separate strings
Jamie Taylor
@Jamie: This will return an array of 4 strings. So use `ForEach` to iterate the resulting array and write an item
abatishchev
How do I do the `ForEach` to write a new string? And then show those strings in different labels?
Jamie Taylor
@Jamie: See my updated post
abatishchev
@Jamie: Glad it helped! (just delete wrong comment)
abatishchev
Thanks for the help
Jamie Taylor
+1  A: 

Use the split function

This is the use i recommend is below, you can adapt it by either:

  • adding more separators (add strings to the separator array like this: New String() {" - ", " _ "} )
  • removing empty entries (not necessary, but usually useful)
Dim MyString As String = "value1 - value2 - value3 -  - value4"
Dim results As String() = MyString.Split(New String() {" - "}, StringSplitOptions.RemoveEmptyEntries)
' you get an array with 4 entries containing each individual string

samy