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("/"))
Dim strSplit() As String = _ myString.Split(Char.Parse("/"))
Dim strSplit() As String = myString.Split("/"c)
Other post in VB.NET
- Assign Multiple enum Values To One Variable - November 2nd, 2007
- Object Initialization in CSharp 3.0 and VB.NET 9 - November 28th, 2007
- The VB.NET Ternary Operator - November 29th, 2007
- UnauthorizedAccessException writing to HKLM - April 8th, 2008
- Overriding Events in VB.NET - May 8th, 2008
- VB.NET - Char from String with Option Strict - April 8th, 2009
- .Net String Pool – Not Just For The Compiler - April 22nd, 2009
- Manually Adding Event Handlers in VB.NET - July 15th, 2009
- VB.NET Processing Before WinForm Display - August 6th, 2009
- Decimal vs Float (Single) or Double - September 18th, 2012
- VB.NET Hide Module Name - August 28th, 2013
- VB.NET Nullable Types - January 15th, 2014