String length when converting from an array of characters

I'm having serious string processing issues. Since my problems are pretty hard to describe, I'll start with a demo code that reproduces them:

Dim s1 As String = "hi"
Dim c(30) As Char
c(0) = "h"
c(1) = "i"
Dim s2 As String = CStr(c)
s2 = s2.Trim()
If not s1 = s2 Then
   MsgBox(s1 + " != " + s2 + Environment.NewLine + _
          "Anything here won't be printed anyway..." + Environment.NewLine + _ 
          "s1.length: " + s1.Length.ToString + Environment.NewLine + _
          "s2.length: " + s2.Length.ToString + Environment.NewLine)
End If                    

A result message box will appear:

screenshot of the messagebox showing only hi! = hi but not the rest of the text

The reason for the failure of this comparison is that s2 has a length of 31 (of the original size of the array), while s1 has a length of 2.

I often came across this problem when reading string information from byte arrays, for example, when processing ID3Tags from MP3 files or other encoded (ASCII, UTF8, ...) information with a given length.

Is there a quick and clean way to prevent this problem?

What is the easiest way to "trim" s2 in a line shown by a debugger?

Thanks in advance, Janis

+4
5

:

Dim myChars(30) As Char
myChars(0) = "h"c           ' cannot convert string to char
myChars(1) = "i"c           ' under option strict (narrowing)
Dim myStrA As New String(myChars)
Dim myStrB As String = CStr(myChars)

:

. 2 NET-, - VB. , , 31 , 2 :

- null/Nothing, Char Chr(0) NUL. NUL String, NUL Console, MessageBox .. , , .


, , char, - . NUL Char, :

Console.WriteLine(myStrA.Length)     ' == 31

, Trim ? MSDN ( Intellisense) :

[Trim] String.

/Chr (0) , Tab, Lf, Cr Space, .

String.Trim , :

myStrA = myStrA.Trim(Convert.ToChar(0))
' using VB namespace constant
myStrA = myStrA.Trim( Microsoft.VisualBasic.ControlChars.NullChar)

:

' nuls and spaces:
myStrA = myStrA.Trim(Convert.ToChar(0), " "c)

/ char:

    For n As Int32 = 0 To myStrA.Length
        Console.Write("{0} is '{1}'", n, myStrA(n))  ' or myStrA.Chars(n)
    Next

0 - 'h'
1 - "i"
2 - '

( CRLF.) char, :

   myStrA(2) = "!"c

, .

. :

ASCII

+7

, ID3v2.4.0 ISO-8859, :

    Dim s1 As String = "Test"
    Dim b() As Byte = New Byte() {84, 101, 115, 116, 0, 0, 0}
    Dim s2 As String = System.Text.ASCIIEncoding.ASCII.GetString(b).Trim(ControlChars.NullChar)

    If s1 = s2 Then Stop

http://id3.org/id3v2.4.0-structure , , .

+2

, CStr(c) NUL (0) . String.Trim() , NUL .

- ( ) NUL ( 0); TakeWhile .

Const NUL as Char = Microsoft.VisualBasic.ChrW(0)
Dim cleanChars() as Char = _
    c.TakeWhile(Function(v, i) v <> NUL) _
     .ToArray

CStr(cleanChars) ' -> "hi"

Bytes ( Chars), , Encoding.GetString, / ,

Encoding.UTF8.GetString(cleanBytes) ' -> still "hi"
+1

Dim, ReDim char, s1.

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

. , 30 "c" .

, , , ​​ CStr, .

,

Dim c(30)

,

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

If you increase, you may be preceded by a save keyword that will expand the array while retaining its current contents.

ReDim Preserve c(s1.Length)
0
source

you may have to set s2 to the same length as s1

    dim s2 as string...
    s2.length = s1.length

hope this helps, ISEA

-6
source

Source: https://habr.com/ru/post/1545918/


All Articles