none
How to assign a Character to specific index of array RRS feed

  • Question

  • 'I declared a string

    Dim str1 as String = "WEB"

    Dim str2 as String

    'i want to put letters to this string one by one like

    str2(0) = str1(0)

    str2(1) = str1(1)

    str2(2) = str1(2)

    Msgbox(str2)

    expected output is "WEB"

    ITS NOT WORKING, whats the problem here?


    Saturday, February 25, 2017 2:37 PM

Answers

  • I tried to use this and works fine to my requirement. thanks
            Dim str1 As String = "SPIDER WEB"
            Dim str2 As String = ""

            str2 = str2 + str1(0)
            str2 = str2 + str1(3)
            str2 = str2 + str1(5)

            MsgBox(str2)

    Your answer rewritten using the string concatenation operator and the shorthand method.

            Dim str1 As String = "SPIDER WEB"
            Dim str2 As String = ""
    
            str2 &= str1(0)
            str2 &= str1(3)
            str2 &= str1(5)
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    • Marked as answer by MetalZERO-1 Saturday, February 25, 2017 3:42 PM
    Saturday, February 25, 2017 3:31 PM

All replies

  • See if this fits your needs

    Dim str1 As String = "WEB"
    Dim str2 As String
    str2 = $"{str1(0)}{str1(1)}{str1(2)}"
    MsgBox(str2)

    Or

    Dim str1 As String = "WEB"
    Dim str2 As String = ""
    For Each c As Char In str1
        str2 &= c
    Next
    MsgBox(str2)


    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 MetalZERO-1 Saturday, February 25, 2017 3:01 PM
    • Unmarked as answer by MetalZERO-1 Saturday, February 25, 2017 3:03 PM
    • Marked as answer by MetalZERO-1 Saturday, February 25, 2017 3:24 PM
    • Unmarked as answer by MetalZERO-1 Saturday, February 25, 2017 3:42 PM
    Saturday, February 25, 2017 2:47 PM
    Moderator
  • the firs choice seems what i need but is has syntax error saying character is not valid at $"{str1(0)}{str1(1)}{str1(2)}"
    Saturday, February 25, 2017 2:53 PM
  • The problem is that str2 has no character array to assign the characters from str1 into.  You need to initialize str2 with enough characters for the assignments to work.  You could initialize str2 to a string of space characters (or anything else).  For example:

    Dim str2 As String = Space(str1.Length)

    I'm curious as to why you want to do this as opposed to just setting str2 = str1...


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, February 25, 2017 3:03 PM
    Moderator
  • Actually my idea is to copy the some of letters from one string to another

    str1 = "SPIDER WEB"

    str2(0) = str1(0)

    str2(0) = str1(3)

    str2(0) = str1(5)

    to make the str2 to "SDR"

    Saturday, February 25, 2017 3:14 PM
  • I tried to use this and works fine to my requirement. thanks
            Dim str1 As String = "SPIDER WEB"
            Dim str2 As String = ""

            str2 = str2 + str1(0)
            str2 = str2 + str1(3)
            str2 = str2 + str1(5)

            MsgBox(str2)
    • Marked as answer by MetalZERO-1 Saturday, February 25, 2017 3:23 PM
    • Unmarked as answer by MetalZERO-1 Saturday, February 25, 2017 3:42 PM
    Saturday, February 25, 2017 3:17 PM
  • I tried to use this and works fine to my requirement. thanks
            Dim str1 As String = "SPIDER WEB"
            Dim str2 As String = ""

            str2 = str2 + str1(0)
            str2 = str2 + str1(3)
            str2 = str2 + str1(5)

            MsgBox(str2)

    Your answer rewritten using the string concatenation operator and the shorthand method.

            Dim str1 As String = "SPIDER WEB"
            Dim str2 As String = ""
    
            str2 &= str1(0)
            str2 &= str1(3)
            str2 &= str1(5)
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    • Marked as answer by MetalZERO-1 Saturday, February 25, 2017 3:42 PM
    Saturday, February 25, 2017 3:31 PM
  • Then what you really want is to make str2 an instance of StringBuilder and append the various characters to it.

    In truth, it won't make much difference until you start doing more than about six concatenations.  Somewhere after that point the &= will get slower and slower compared to using StringBuilder.Append().


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, February 25, 2017 4:16 PM
    Moderator
  • the firs choice seems what i need but is has syntax error saying character is not valid at $"{str1(0)}{str1(1)}{str1(2)}"
    It's called string interpolation, for VS2015 and higher.

    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

    Saturday, February 25, 2017 4:59 PM
    Moderator