3

Data examples:

  • EFT- MONTREAL || 378 STREET AVE
  • EFT TORONTO1 || TESTING 875 RD ADDRESS

What I need extract from these:

  • EFT- MONTREAL
  • EFT TORONTO1 (note: end of this line is extra space)

Is this the correct way to extract?
String.Split(" || ")(0)

The problem is, sometimes I get these values saved to Database:

  • EFT-
  • EFT

I just want to know which way is the correct one so I can change every line to the correct one.

skmasq
  • 4,470
  • 6
  • 42
  • 77

1 Answers1

5

No, there is no overload that accepts a string. So you are actually using the overload of String.Split which accepts a ParamArray Char(). But this will check if any of these chars match, so not only if the whole sub-string matches. It is the same as this:

 Dim first = text.Split(" "c, "|"c, "|"c, " "c)(0) ' "EFT-"

Use this instead:

Dim first = text.Split({" || "}, StringSplitOptions.RemoveEmptyEntries)(0)

Note that you can also use StringSplitOptions.None, that's not the problem.

String.Split Method(String(), StringSplitOptions)

Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. A parameter specifies whether to return empty array elements.

But note that you should set OPTION STRICT to on, then your code would not even compile since a string is not a Char() implicitly. With OPTION STRICT off it will be converted silently.

Option Strict on by default in VB.NET

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I'm sorry for Ignorance where should I set `OPTION STRICT off` ? – skmasq Dec 05 '13 at 15:18
  • @skmasq: You can set it in the file at the top: `Option Strict On`, in the project settings or globally(recommended). Therefore you have to change it in Visual Studio. http://stackoverflow.com/questions/5160669/option-strict-on-by-default-in-vb-net – Tim Schmelter Dec 05 '13 at 15:20
  • What should I do if this `.vb` is compiled in run time in IIS? – skmasq Dec 05 '13 at 15:22
  • @skmasq: That doesn't matter. Just change it in the project or as default and compile your project. I'm afraid that you have to fix many errors first. Then deploy it and you're fine. – Tim Schmelter Dec 05 '13 at 15:24