Split words but preserve special words

I am using VS2012, vb.net.

I am wanting to load a string into an array. The string is a series of words that are separated by a space (" ").

Currently I am using the following code:

Dim stringTempCurrentGameTypes() As String = Split(stringTypeList, " ")

This works perfectly. However, I have an exception to some of the data that gets loaded into the array. Sometimes the data in the string (the string that is separated by spaces) has two words that I want to load into the array as one item, and not two items.

Here is an example of a string that I am talking about:

tourney ffa team ctf clan arena test

The exception is the two words 'clan arena'.

Currently, if I just use the split command, I get an array with the following elements:

item(0) = tourney 
item(1) = ffa
item(2) = team
item(3) = ctf
item(4) = clan
item(5) = arena
item(6) = test

I am after the following:

item(0) = tourney 
item(1) = ffa
item(2) = team
item(3) = ctf
item(4) = clan arena
item(5) = test

How can I detect if the item being added to the array is the words 'clan arena', and add this as one entry, rather than as two entries? Also, the words 'clan arena' may change, so rather than hard coding the words 'clan arena', I need to do it via a string variable.

Answers


There are, of course, several ways to do this.

One way is to replace the whitespace of all your special items in the input string with a temporary character, split the input string, and then change the temporary character back to the original whitespace.


Example:

Dim raw = "tourney ffa team ctf clan arena test"
Dim special_words = new String() {"clan arena"}
Dim tmp_char = "$"

For Each word in special_words
    raw = raw.Replace(word, word.Replace(" ", tmp_char))
Next

Dim result = raw.Split(new Char() {" "c})
For i = 0 To result.Count -1 
    result(i) = result(i).Replace(tmp_char, " ")
Next

As temporary character, you could use a unprintable character like Chr(31) (unit seperator) or anything you know that would not be in your input string.

This approach is quite simple and preserves the order of your items.


Need Your Help

Storing the clicked value in radio buttonswith cookies

javascript html

Hey guys am new to js actually..I have two radio buttons and a save button..The code i have done

CSS styling multiple elements

css css-selectors

quick question regarding CSS. I am currently working on a project that has a few CSS files that style the pages. At the minute I am trying to figure the best way of re-styling several elements ie: ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.