Home » VB.NET » VB.NET – Char from String with Option Strict

VB.NET – Char from String with Option Strict

G04B0079 So here’s the question:

I’m using String.Split() and need to pass in a Char or a Char array as the parameter.  If I pass in a string String.Split(“/”) I get an error “Option Strict On disallows implicit conversions from ‘String’ to ‘Char’.”

Obviously, the easiest way to fix this would be to turn off Option Strict, but I would prefer to keep it on.  So how do I pass in the Char instead of a String in this situation?”

There are actually several ways to accomplish what you are trying to do.

The first and most general solution would be to call the ToCharArray() method off the string.

Dim strSplit() As String = myString.Split("/".ToCharArray())

The advantage to this method is that it will work regardless of what size the string is and it will use each character in the string as a delimiter.

But what if you only have one character in your array?  Surely there is a shorter, cleaner statement we can use.

As a matter of fact, there are several other options.  You could use Convert.ToChar() or Char.Parse()

Dim strSplit() As String = _
   myString.Split(Convert.ToChar("/"))

or

Dim strSplit() As String = _
   myString.Split(Char.Parse("/"))

But the easiest way to convert a single character string to a Char is simply to put a “c” after the closing quote:

Dim strSplit() As String = myString.Split("/"c)

 

Other post in VB.NET

Related Post

  • .Net String Pool – Not Just For The Compiler.Net String Pool – Not Just For The Compiler On Monday, I was corrected in my assertion that creating multiple empty strings would create multiple objects.  Turns out the compiler automatically puts all of the strings that are […]
  • VB.NET Nullable TypesVB.NET Nullable Types SQL has long had the ability to specify that a value is NULL even if it is a primitive type, but the only way you could have a NULL value in VB.NET is if you were dealing with an […]
  • Manually Adding Event Handlers in VB.NETManually Adding Event Handlers in VB.NET Typically when we write our code, the event handlers get wired up for us using the handles clause.  So we never have to worry about wiring up our event handlers manually. But what […]
  • VB.NET Processing Before WinForm DisplayVB.NET Processing Before WinForm Display I woke up this morning to an interesting question. “Using VB.net 2008, I want my project to be a Windows Forms Application, but upon startup, I want to check a few files to see if they […]
  • String and StringBuilderString and StringBuilder A couple of weeks ago, we discussed Value types and Reference types where we said that a reference type points to the value it represents and a value type is the value it […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer focusing on ASP.NET, C#, Node.js, JavaScript, HTML, CSS, BootStrap, and Angular.JS. Does your team need additional help in any of the above? Contact Dave today.

  • That’s cool! tnx