String length when converting from a character array

后端 未结 4 654
耶瑟儿~
耶瑟儿~ 2020-12-06 20:08

I\'m having serious problems with string-handling. As my problems are rather hard to describe, I will start with some demo code reproducing them:

Dim s1 As S         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 21:07

    You can either Dim or ReDim the char array once you know the length of the s1 string.

    Dim s1 As String
    s1 = "hi"
    Dim c(s1.Length) As Char
    c(0) = "h"
    c(1) = "i"
    Dim s2 As String = CStr(c)
    

    And now your comparison will work no matter the length of the original string. You didn't state whether the length of 30 for 'c' is a requirement or not here.

    But even if it was, you'd still need to either expand or contract the array to have the same CStr length to do your comparison.

    So even after declaring

    Dim c(30)
    

    You can later in the code block redimension the array like this

    ReDim c(s1.Length) 'Or any int value you like
    

    If increasing you can precede with the preserve keyword, which will expand the array while maintaining its current contents.

    ReDim Preserve c(s1.Length)
    

提交回复
热议问题