none
Use Sting as Char RRS feed

  • Question

  • Hi,

    .Split(",") is not accepted because of implicit conversions from 'String' to 'Char' so I was suggested to use .Split(","c) instead.

    I can also use it for:

    Char.IsLetterOrDigit(","c)

    But it does NOT work for below usage:

    Char.IsLetterOrDigit(TextBox.Text.Substring(MyLoop, 1)c)

    Still implicit conversions from 'String' to 'Char' is shown!

    Please help.

    Tuesday, February 19, 2019 1:13 PM

Answers

  • Hello,

    You could convert your string to a character array then do assertion.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim itemArray = TextBox1.Text.ToCharArray()
    
            Console.WriteLine("Char   IsLetter  IsDigit  IsWhitespace")
    
            For Each currentCharacter As Char In itemArray
    
                Console.WriteLine(
                    $"'{currentCharacter}' " &
                    $"{currentCharacter.IsLetter(currentCharacter),8} " &
                    $"{currentCharacter.IsDigit(currentCharacter),9} " &
                    $"{currentCharacter.IsWhiteSpace(currentCharacter),8}")
    
            Next
            Console.WriteLine()
            Console.WriteLine("First char letter")
    
            Dim firstItemTest = Char.IsLetter(itemArray(0))
            Console.WriteLine(firstItemTest)
    
            Console.WriteLine()
            Console.WriteLine("First char diget")
    
            Dim secondtest = Char.IsDigit(itemArray(0))
            Console.WriteLine(secondtest)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by OSVBNET Tuesday, February 19, 2019 4:05 PM
    Tuesday, February 19, 2019 2:40 PM
    Moderator
  • Dear Karen, have a question: You told me for .Split(",") in order to prevent error I use .Split(","c)

    What is that c added after string to make it char? A short form of a command? Thanks.

    Yes the character -c- is to designate a char while in C# this is the syntax.

    string someValue = "A,B,C";
    string[] result = someValue.Split(',');

    Back to VB.NET, are you aware Split is rigged for multiple delimiters? here space or comma

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim someValue = "A,B,C D"
            Dim result = someValue.
                    Split(
                        New Char() {CChar(vbTab), CChar(" "), CChar(",")})
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by OSVBNET Tuesday, February 19, 2019 7:07 PM
    Tuesday, February 19, 2019 4:53 PM
    Moderator
  • Thanks Karen, So I can also use Convert.ToChar as well as CChar to be more .NET? Like below solution?

    dbasnett when I turned Option Explicit On my project returned around 250 errors, all these questions were about eliminating the errors! Now I use:

    Char.IsLetterOrDigit(PasswordTextBox.Text.Substring(MyLoop, 1))
    And: Error: Option Strict On disallows implicit conversions from 'String' to 'Char'.

    So I updated to:

    Char.IsLetterOrDigit(Convert.ToChar(PasswordTextBoxX.Text.Substring(MyLoop, 1)))
    And error is gone!

    Why make this so difficult????

    Try this please

    Char.IsLetterOrDigit(PasswordTextBox.Text(MyLoop))


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    • Marked as answer by OSVBNET Wednesday, February 20, 2019 1:01 AM
    Tuesday, February 19, 2019 8:30 PM

All replies

  • Hi,

    .Split(",") is not accepted because of implicit conversions from 'String' to 'Char' so I was suggested to use .Split(","c) instead.

    I can also use it for:

    Char.IsLetterOrDigit(","c)

    But it does NOT work for below usage:

    Char.IsLetterOrDigit(TextBox.Text.Substring(MyLoop, 1)c)

    Still implicit conversions from 'String' to 'Char' is shown!

    Please help.

    Not clear what you want so a guess.

    If MyLoop contains the character index you want to check then simply access that character.  A string is an array of characters.

            For MyLoop As Integer = 0 To TextBox1.TextLength - 1
                Debug.WriteLine(Char.IsLetterOrDigit(TextBox1.Text(MyLoop)))
            Next
    


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Tuesday, February 19, 2019 1:39 PM
  • Hi and thanks, but you misunderstood

    If I use:

    Char.IsLetterOrDigit("T")

    It is fine, but if I turn on Option Strict, which I HAVE TO, I will get error:

    Error: Option Strict disallows implicit conversions from 'String' to 'Char'

    One solution is to use it like:

    Char.IsLetterOrDigit("T"c)

    The problem is that, my string is not simply a constant, like "T" in above sample, it's a character of Text inside the TextBox so I get it in code:

    Again this one is also OK:

    Char.IsLetterOrDigit(PasswordTextBoxX.Text.Substring(MyLoop, 1))

    But when I turn on Option Strict, I get the same error again:

    Error: Option Strict disallows implicit conversions from 'String' to 'Char'

    The question is that for this one, I just cannot use a "c" afterwards like 1st sample to eliminate the error, if I use:

    Char.IsLetterOrDigit(PasswordTextBoxX.Text.Substring(MyLoop, 1)c)

    There is still error, how to solve it?

    Tuesday, February 19, 2019 1:59 PM
  • Hello,

    You could convert your string to a character array then do assertion.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim itemArray = TextBox1.Text.ToCharArray()
    
            Console.WriteLine("Char   IsLetter  IsDigit  IsWhitespace")
    
            For Each currentCharacter As Char In itemArray
    
                Console.WriteLine(
                    $"'{currentCharacter}' " &
                    $"{currentCharacter.IsLetter(currentCharacter),8} " &
                    $"{currentCharacter.IsDigit(currentCharacter),9} " &
                    $"{currentCharacter.IsWhiteSpace(currentCharacter),8}")
    
            Next
            Console.WriteLine()
            Console.WriteLine("First char letter")
    
            Dim firstItemTest = Char.IsLetter(itemArray(0))
            Console.WriteLine(firstItemTest)
    
            Console.WriteLine()
            Console.WriteLine("First char diget")
    
            Dim secondtest = Char.IsDigit(itemArray(0))
            Console.WriteLine(secondtest)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by OSVBNET Tuesday, February 19, 2019 4:05 PM
    Tuesday, February 19, 2019 2:40 PM
    Moderator
  • Thank you very much indeed :)
    Tuesday, February 19, 2019 4:06 PM
  • Dear Karen, have a question: You told me for .Split(",") in order to prevent error I use .Split(","c)

    What is that c added after string to make it char? A short form of a command? Thanks.

    Tuesday, February 19, 2019 4:11 PM
  • Hi and thanks, but you misunderstood

    If I use:

    Char.IsLetterOrDigit("T")

    It is fine, but if I turn on Option Strict, which I HAVE TO, I will get error:

    Error: Option Strict disallows implicit conversions from 'String' to 'Char'

    One solution is to use it like:

    Char.IsLetterOrDigit("T"c)

    The problem is that, my string is not simply a constant, like "T" in above sample, it's a character of Text inside the TextBox so I get it in code:

    Again this one is also OK:

    Char.IsLetterOrDigit(PasswordTextBoxX.Text.Substring(MyLoop, 1))

    But when I turn on Option Strict, I get the same error again:

    Error: Option Strict disallows implicit conversions from 'String' to 'Char'

    The question is that for this one, I just cannot use a "c" afterwards like 1st sample to eliminate the error, if I use:

    Char.IsLetterOrDigit(PasswordTextBoxX.Text.Substring(MyLoop, 1)c)

    There is still error, how to solve it?

    If you want one character of a TextBox, and know which character the code I posted is correct.  You are using .Substring to get one character at MyLoop.  Don't use substring.

    Use this

    Char.IsLetterOrDigit(TextBox1.Text(MyLoop))


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    Tuesday, February 19, 2019 4:41 PM
  • The c suffix indicates a Char literal. When Option Strict is On most literals require a type character(s) to be included.

    Constant and Literal Data Types (Visual Basic)

    To convert a String (of a single character) to Char you can cast using CType().

    CType(yourString.Substring(yourIndex, 1), Char)

    For example:

            Dim yourString As String = "hello--123"
    
            For yourIndex As Integer = 0 To yourString.Length - 1
                result = Char.IsLetterOrDigit(CType(yourString.Substring(yourIndex, 1), Char))
    
                Console.WriteLine("{0} IsLetterOrDigit: {1}", yourString.Substring(yourIndex, 1), result)
            Next


    Tuesday, February 19, 2019 4:43 PM
  • Dear Karen, have a question: You told me for .Split(",") in order to prevent error I use .Split(","c)

    What is that c added after string to make it char? A short form of a command? Thanks.

    Yes the character -c- is to designate a char while in C# this is the syntax.

    string someValue = "A,B,C";
    string[] result = someValue.Split(',');

    Back to VB.NET, are you aware Split is rigged for multiple delimiters? here space or comma

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim someValue = "A,B,C D"
            Dim result = someValue.
                    Split(
                        New Char() {CChar(vbTab), CChar(" "), CChar(",")})
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by OSVBNET Tuesday, February 19, 2019 7:07 PM
    Tuesday, February 19, 2019 4:53 PM
    Moderator
  • The c suffix indicates a Char literal. When Option Strict is On most literals require a type character(s) to be included.

    Constant and Literal Data Types (Visual Basic)

    To convert a String (of a single character) to Char you can cast using CType().

    CType(yourString.Substring(yourIndex, 1), Char)

    For example:

            Dim yourString As String = "hello--123"
    
            For yourIndex As Integer = 0 To yourString.Length - 1
                result = Char.IsLetterOrDigit(CType(yourString.Substring(yourIndex, 1), Char))
    
                Console.WriteLine("{0} IsLetterOrDigit: {1}", yourString.Substring(yourIndex, 1), result)
            Next


    Why make this more complicated than it is. A string is an array of char.  If you know the index of the character you want then...

            Dim result As Boolean
            Dim yourString As String = "hello--123"
            For yourIndex As Integer = 0 To yourString.Length - 1
                result = Char.IsLetterOrDigit(yourString(yourIndex))
                Console.WriteLine("{0} IsLetterOrDigit: {1}", yourString.Substring(yourIndex, 1), result)
            Next

    You don't have to do substring, convert to char, just access the character of the string directly.


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein


    • Edited by dbasnett Tuesday, February 19, 2019 5:10 PM
    Tuesday, February 19, 2019 5:05 PM
  • Thanks Karen, So I can also use Convert.ToChar as well as CChar to be more .NET? Like below solution?

    dbasnett when I turned Option Explicit On my project returned around 250 errors, all these questions were about eliminating the errors! Now I use:

    Char.IsLetterOrDigit(PasswordTextBox.Text.Substring(MyLoop, 1))
    And: Error: Option Strict On disallows implicit conversions from 'String' to 'Char'.

    So I updated to:

    Char.IsLetterOrDigit(Convert.ToChar(PasswordTextBoxX.Text.Substring(MyLoop, 1)))
    And error is gone!
    • Edited by OSVBNET Tuesday, February 19, 2019 7:24 PM
    Tuesday, February 19, 2019 7:24 PM
  • Thanks Karen, So I can also use Convert.ToChar as well as CChar to be more .NET? Like below solution?

    dbasnett when I turned Option Explicit On my project returned around 250 errors, all these questions were about eliminating the errors! Now I use:

    Char.IsLetterOrDigit(PasswordTextBox.Text.Substring(MyLoop, 1))
    And: Error: Option Strict On disallows implicit conversions from 'String' to 'Char'.

    So I updated to:

    Char.IsLetterOrDigit(Convert.ToChar(PasswordTextBoxX.Text.Substring(MyLoop, 1)))
    And error is gone!

    Why make this so difficult????

    Try this please

    Char.IsLetterOrDigit(PasswordTextBox.Text(MyLoop))


    Search Documentation

    SerialPort Info

    Multics - An OS ahead of its time.

     "Those who use Application.DoEvents have no idea what it does

        and those who know what it does never use it."    former MSDN User JohnWein

    • Marked as answer by OSVBNET Wednesday, February 20, 2019 1:01 AM
    Tuesday, February 19, 2019 8:30 PM
  • Interesting, thanks man :)

    It means that in this loop:

    For MyLoop As Integer = 0 To PasswordTextBox.Text.Length - 1

    Using:
    PasswordTextBox.Text.Substring(MyLoop, 1)
    &
    PasswordTextBoxX.Text(MyLoop)

    Are same! Smart :)

    • Edited by OSVBNET Wednesday, February 20, 2019 1:07 AM
    Wednesday, February 20, 2019 1:01 AM