从字符数组转换时的字符串长度 [英] String length when converting from a character array

查看:215
本文介绍了从字符数组转换时的字符串长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理字符串方面遇到严重问题。
由于我的问题很难描述,因此我将以一些演示代码来再现它们:

  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()
如果不是s1 = s2,则
MsgBox(s1 +!= + s2 + Environment.NewLine + _
仍然无法打印... + Environment.NewLine + _
s1.length: + s1.Length.ToString + Environment.NewLine + _
s2.length: + s2。 Length.ToString + Environment.NewLine)
结束于

结果消息框如下所示:






此比较失败的原因是s2的长度为31(从原始数组大小开始),而s1的长度为2。



当从字节数组中读取字符串信息时,我经常会遇到这种问题,例如例如,当处理来自MP3的ID3Tag或具有预先指定长度的其他编码(ASCII,UTF8等)信息时。



是否有任何快速,干净的方法来防止这种情况问题?



最简单的方法是将s2修剪到调试器显示的字符串上?

解决方案

为清楚起见,我更改了变量名称:

  Dim myChars(30)As Char 
myChars(0)= h c'无法将字符串转换为char
myChars(1)= i c'在选项严格(变窄)下
Dim myStrA作为新字符串(myChars)
Dim myStrB As String = CStr(myChars)

简短的答案是这样的:



在内部,字符串字符数组。最后两行都使用NET代码创建一个字符串,另一行使用VB函数。事实是,尽管数组有31个元素,但仅初始化了2个





但是, String.Trim 具有 overload ,可让您指定要删除的字符:

  myStrA = myStrA.Trim(Convert.ToChar(0))
'使用VB命名空间常量
myStrA = myStrA.Trim(Microsoft.VisualBasic.ControlChars .NullChar)

您可以指定多个字符:

 'nul和空格:
myStrA = myStrA.Trim(Convert.ToChar(0), c)






字符串 可以被索引/迭代为char数组:

 对于InAs32 = 0到myStrA.Length 
Console.Write( {0} is'{1}', n,myStrA(n))'或myStrA.Chars(n)
下一个




0是'h'

1是'i'

2是'


(输出窗口将不会您甚至无法更改字符串的char数组来更改字符串数据,但是:

  myStrA(2) =! c 

由于它们是只读的,因此不会编译。



另请参见:



ASCII表


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 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                    

The result messagebox looks like this:

The reason that this comparison fails is that s2 has the length 31 (from the original array-size) while s1 has the length 2.

I stumble over this kind of problem quite often when reading string-information out of byte-arrays, for example when handling ID3Tags from MP3s or other encoded (ASCII, UTF8, ...) information with pre-specified length.

Is there any fast and clean way to prevent this problem?

What is the easiest way to "trim" s2 to the string shown by the debugger?

解决方案

I changed the variable names for clarity:

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)

The short answer is this:

Under the hood, strings are character arrays. The last 2 lines both create a string one using NET code, the other a VB function. The thing is that, although the array has 31 elements, only 2 were initialized:

The rest are null/Nothing, which for a Char means Chr(0) or NUL. Since NUL is used to mark the end of a String, only the characters up to that NUL will print in the Console, MessageBox etc. Text appended to the string will not display either.


Concepts

Since the strings above are created directly from a char array, the length is that of the original array. The Nul is a valid char so they get added to the string:

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

So, why doesn't Trim remove the nul characters? MSDN (and Intellisense) tells us:

[Trim] Removes all leading and trailing white-space characters from the current String object.

The trailing null/Chr(0) characters are not white-space like Tab, Lf, Cr or Space, but is a control character.

However, String.Trim has an overload which allows you to specify the characters to remove:

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

You can specify multiple chars:

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


Strings can be indexed / iterated as a char array:

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

0 is 'h'
1 is 'i'
2 is '

(The output window will not even print the trailing CRLF.) You cannot change the string's char array to change the string data however:

   myStrA(2) = "!"c

This will not compile because they are read-only.

See also:

ASCII table

这篇关于从字符数组转换时的字符串长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆