摆脱空虚

扫码查看
我尝试使用String.Trim(null)但这并没有达到预期的效果。 我唯一能想到的就是将字符串转换为一个char数组 并循环直到我点击一个空字符。 有没有更好的方法来摆脱空值? 如果我能得到有用的字符串的实际长度。 谢谢, 画了I have been using System.Text.Encoding.Unicode.GetString(byte[],0,72)to converter a byte array from the registry to a string.This works, but I end up with a bunch of null characters afterthe "good" string data that I need to parse. This is evidencedby the fact that string.length returns 36 when the actual stringdata I need to work with is usually much less.I tried using String.Trim(null) but this doesn''t achieve the desired result.The only thing I can think of is to convert the string to a char arrayand loop through until I hit a null character.Is there a better way to get rid of the nulls?If I could just get the actual length of the string that would help.Thanks,Drew推荐答案 Drew, 我相信GetString方法asxsumed你的字节数组包含构建正确字符串所需的 数据,如果没有,那么你将得到 额外 填充。长度来自于你告诉它72个字节,每个 unicode字符每个两个字节 - > 72/2 = 36个字符。 HTH, // Andreas "德鲁" <所以***** @ hotmail.com> skrev i meddelandet 新闻:uJ ************** @ TK2MSFTNGP11.phx.gbl ...Drew,I believe the GetString methods asxsumed your byte array to contain thedata needed to build the correct string, and if not then you will get theextrapadding. The lenght comes from the fact that you tell it 72 bytes and eachunicode character is two bytes each -> 72/2 = 36 characters.HTH,//Andreas"Drew" <so*****@hotmail.com> skrev i meddelandetnews:uJ**************@TK2MSFTNGP11.phx.gbl...我一直在使用System .Text.Encoding.Unicode.GetString(byte [],0,72)将一个字节数组从注册表转换为字符串。 这样可行,但我最终得到一个好之后的一堆空字符我需要解析的字符串数据。事实证明,当我需要处理的实际字符串数据通常少得多时,string.length返回36这个事实。 我尝试使用String.Trim( null)但是这并没有达到所需的结果。我唯一能想到的是将字符串转换为char数组并循环直到我点击空字符。 有没有更好的方法来摆脱空值? 如果我能得到有用的字符串的实际长度。 谢谢, 画了 I have been using System.Text.Encoding.Unicode.GetString(byte[],0,72) to converter a byte array from the registry to a string. This works, but I end up with a bunch of null characters after the "good" string data that I need to parse. This is evidenced by the fact that string.length returns 36 when the actual string data I need to work with is usually much less. I tried using String.Trim(null) but this doesn''t achieve the desiredresult. The only thing I can think of is to convert the string to a char array and loop through until I hit a null character. Is there a better way to get rid of the nulls? If I could just get the actual length of the string that would help. Thanks, Drew德鲁: 看起来安德烈斯已经修复了你的根本问题。如果你还需要 来执行修剪,你可以使用 string newString = oldString.Trim(''\ 0'')(注意单引号,而不是双$ 报价) 我不知道为什么String.Trim(null);不行。根据帮助, 应该是你所追求的。 " Drew" <所以***** @ hotmail.com>在消息中写道 新闻:uJ ************** @ TK2MSFTNGP11.phx.gbl ...Drew:It looks as though Andreas has your root problem fixed. If you still needto perform the trim, you can usestring newString = oldString.Trim(''\0'') (note the single quotes, not doublequotes)I''m not sure why String.Trim(null); doesn''t work. According to the help, itshould be exactly what you are after."Drew" <so*****@hotmail.com> wrote in messagenews:uJ**************@TK2MSFTNGP11.phx.gbl...我一直在用System .Text.Encoding.Unicode.GetString(byte [],0,72)将一个字节数组从注册表转换为字符串。 这样可行,但我最终得到一个好之后的一堆空字符我需要解析的字符串数据。事实证明,当我需要处理的实际字符串数据通常少得多时,string.length返回36这个事实。 我尝试使用String.Trim( null)但是这并没有达到所需的结果。我唯一能想到的是将字符串转换为char数组并循环直到我点击空字符。 有没有更好的方法来摆脱空值? 如果我能得到有用的字符串的实际长度。 谢谢, Drew I have been using System.Text.Encoding.Unicode.GetString(byte[],0,72) to converter a byte array from the registry to a string. This works, but I end up with a bunch of null characters after the "good" string data that I need to parse. This is evidenced by the fact that string.length returns 36 when the actual string data I need to work with is usually much less. I tried using String.Trim(null) but this doesn''t achieve the desiredresult. The only thing I can think of is to convert the string to a char array and loop through until I hit a null character. Is there a better way to get rid of the nulls? If I could just get the actual length of the string that would help. Thanks, Drew Drew< so ***** @ hotmail.com>写道:Drew <so*****@hotmail.com> wrote:我一直在使用System.Text.Encoding.Unicode.GetString(byte [],0,72)将一个字节数组从注册表转换为字符串。 /> 这样可行,但是在好之后我最终得到了一堆空字符。我需要解析的字符串数据。事实证明,当我需要使用的实际字符串数据通常要少得多时,string.length返回36。 如果实际字符串少于36个字符,为什么要解码 72字节?数字72来自哪里? 我尝试使用String.Trim(null)但这并没有达到预期的效果。 不,它不会。空引用与空字符不同, \ 0。 String.Trim(''\''')应该没问题。 我唯一能想到的就是将字符串转换为char数组并循环直到我点击一个空字符。 有没有更好的方法来摆脱空值? 如果我可以得到有用的字符串的实际长度。 I have been using System.Text.Encoding.Unicode.GetString(byte[],0,72) to converter a byte array from the registry to a string. This works, but I end up with a bunch of null characters after the "good" string data that I need to parse. This is evidenced by the fact that string.length returns 36 when the actual string data I need to work with is usually much less.If the actual string is less than 36 characters, why are you decoding72 bytes? Where did the number 72 come from? I tried using String.Trim(null) but this doesn''t achieve the desired result.No, it wouldn''t. A null reference isn''t the same as a null character,\0. String.Trim(''\0'') should be fine though. The only thing I can think of is to convert the string to a char array and loop through until I hit a null character. Is there a better way to get rid of the nulls? If I could just get the actual length of the string that would help. 注册表字符串总是以null为后缀 - 所以你可以只减少两个 字节,以避免在结尾时出现空值字符串。 然而,如果数据 通常少得多,这听起来不是你真正的问题。 - Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 如果回复该群组,请不要给我发邮件Registry strings are always null-suffixed - so you can just decode twobytes fewer in order to avoid having a null at the end of the string.However, it doesn''t sound like this was your real problem, if the datais "usually much less".--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me too 这篇关于摆脱空虚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-27 23:44
查看更多