I'm trying to create a program that determines if what i inputted is a Literal (String or a Char)
My idea on how to create one is if i inputted a string (Starts with the usual ") so the first Index in my Array should be " or else it will show invalid same with the last index it should be ".

So my problem is i don't know how. If i input something in a Textbox (Example: Daniweb) my program should automatical determine that an Array has 6 indexes, Can someone help me.

Dani AI

Generated

Quick summary and clarification (ties to the replies by , and ): treat the TextBox content as a sequence of characters. The number of elements equals the number of characters you actually want to store; because VB uses zero‑based indexing the last index is length − 1. So the literal text Daniweb contains 7 characters and its highest index is 6 (array length 7, indexes 0..6). The community hints — check the first/last character and view a string as a character sequence — are the right starting points.

A practical checklist and pitfalls to handle reliably:

  • Trim accidental outer whitespace but don’t lose spaces intentionally inside quoted literals.
  • If the user typed a quoted literal, remove the surrounding quotes to get the inner content before counting characters.
  • Support common literal forms: VB char literal uses a quoted character followed by c (for example: "a"c), C#/JS char style uses single quotes ('a'), and string literals use double quotes ("text"). Verify the inner content length for char literals (usually one character).
  • Watch malformed input: unmatched quotes, empty quotes, or multi-character content with a trailing c should be treated as invalid or plain text depending on desired behavior.
  • If full language-literal parsing is required (escaped quotes, Unicode escapes), use a small parser or a regex tuned for the target escape rules rather than only checking first/last characters.

Example helper (VB.NET): this routine classifies the input, returns the kind and the character count / last index for the inner content.

Function AnalyzeInput(input As String) As Tuple(Of String, Integer, Integer)
    If String.IsNullOrEmpty(input) Then Return Tuple.Create("Empty", 0, -1)
    Dim s = input.Trim()
    Dim dq As Char = ChrW(34) ' "
    Dim sq As Char = "'"c      ' '
    ' VB char: "x"c
    If s.Length >= 4 AndAlso s(0) = dq AndAlso (s(s.Length - 1) = "c"c OrElse s(s.Length - 1) = "C"c) AndAlso s(s.Length - 2) = dq Then
        Dim inner = s.Substring(1, s.Length - 3)
        If inner.Length = 1 Then Return Tuple.Create("Char", 1, 0)
    End If
    ' C#-style char: 'x'
    If s.Length >= 3 AndAlso s(0) = sq AndAlso s(s.Length - 1) = sq Then
        Dim inner = s.Substring(1, s.Length - 2)
        If inner.Length = 1 Then Return Tuple.Create("Char", 1, 0)
    End If
    ' Double-quoted string
    If s.Length >= 2 AndAlso s(0) = dq AndAlso s(s.Length - 1) = dq Then
        Dim inner = s.Substring(1, s.Length - 2)
        Return Tuple.Create("String", inner.Length, Math.Max(-1, inner.Length - 1))
    End If
    ' Plain text
    Return Tuple.Create("Plain", s.Length, Math.Max(-1, s.Length - 1))
End Function

Note: this is a pragmatic helper. If input must accept escaped sequences (e.g. \" or doubled "" in VB), extend the routine with a parser or regex that understands those escapes.

Recommended Answers

All 4 Replies

Use substr() method e.g. if textBox1.Text.Substring(0,1) = '"' then

Thanks for replying but i have one problem, It's not dynamic.
What if i type something that has larger numbers and letters and the one in the code is fixed it will give me Invalid.

Use the Textbox1.Text.Length property.

Any string is basically a character array. You can read the individual characters with an index, Textbox1.Text(0). The last character, without using LINQ, like this, Textbox1.Text(TextBox1.Text.Length-1). With LINQ, TextBox1.Text.Last().

A simple conditional statement to test, If Not(TextBox1.Text(0) = """"c) Or Not(TextBox1.Text(TextBox1.Text.Length-1) = """"c) Then.

Thank you every much.
I was so fixed on using something more complicated like what i've suggested with the indexes and arrays
whereas i didn't even know there's a Property like that, I really need to read more.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.