vb.net 如何在vb.net中声明一个固定长度的字符串而不在赋值过程中增加长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16996971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:53:08  来源:igfitidea点击:

How to declare a fixed length string in vb.net without increasing the length during assignment

vb.net

提问by Arcadian

I have a fixed length string variable:

我有一个固定长度的字符串变量:

<VBFixedString(10)>
Public myVar As String

When I assign the var to a value that has 11 chars the var changes length.

当我将 var 分配给具有 11 个字符的值时,var 会改变长度。

This is what I get:

这就是我得到的:

  myvar = "1234567890ABCD" ' result = myvar being "1234567890ABCD"

What I want is myvar to be: "1234567890"

我想要的是 myvar 是:“1234567890”

and then if it is less than 10 I would like

然后如果它小于 10 我想要

myvar = "12345" to be "12345     "

I can do this with PadLeft PadRight but was wondering if I can declare it as a fixed length and it would handle all that for you.

我可以用 PadLeft PadRight 做到这一点,但想知道我是否可以将它声明为固定长度,它会为你处理所有这些。

回答by MotoSV

From what I understand the VBFixedStringattribute is only recognised by certain file based methodsto help structure content written to/read from files. The compiler will not use that attribute for anything else, including to alter how a variable assignment is compiled.

据我所知,该VBFixedString属性只能被某些基于文件的方法识别,以帮助构建写入/读取文件的内容。编译器不会将该属性用于其他任何事情,包括更改变量赋值的编译方式。

Taken from MSDN:

摘自 MSDN:

The VBFixedStringAttribute is informational and cannot be used to convert a variable length string to a fixed string. The purpose of this attribute is to modify how strings in structures and non-local variables are used by methods or API calls that recognize the VBFixedStringAttribute. Keep in mind that this attribute does not change the actual length of the string itself.

VBFixedStringAttribute 是信息性的,不能用于将可变长度字符串转换为固定字符串。此属性的目的是修改识别 VBFixedStringAttribute 的方法或 API 调用如何使用结构中的字符串和非局部变量。请记住,此属性不会更改字符串本身的实际长度。

The last sentence is the important bit:

最后一句话很重要:

Keep in mind that this attribute does not change the actual length of the string itself.

请记住,此属性不会更改字符串本身的实际长度。

http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.vbfixedstringattribute.aspx

http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.vbfixedstringattribute.aspx

EDIT 1:

编辑 1:

A quick example on how to auto-padding a string based on a fixed length:

关于如何根据固定长度自动填充字符串的快速示例:

Function FixedLengthString(ByVal value As String, ByVal totalLength As Integer, ByVal padding As Char) As String
        Dim length = value.Length
        If (length > totalLength) Then Return value.Substring(0, totalLength)
        Return value.PadRight(totalLength, padding)
End Function

Here you can pass in a string and if the length of the string is greater than the specified total length you will get a string matching that length. Anything less and you'll get the string plus the padding character upto the specified total length.

在这里您可以传入一个字符串,如果字符串的长度大于指定的总长度,您将得到一个匹配该长度的字符串。任何更少的东西,你都会得到字符串加上填充字符,直到指定的总长度。

This can be improved with error checking and maybe making the method an extension method so you don't have to pass "value".

这可以通过错误检查来改进,并且可能使该方法成为扩展方法,因此您不必传递“值”。

回答by Al Jones

and from the old school, LSET will do exactly what you're asking.

从旧学校开始,LSET 将完全按照您的要求执行。

 myvar = lset("1234567890ABCD",10)

results in "1234567890", while

结果为“1234567890”,而

 myvar = lset("12345",10)

results in "12345___" - sorry, the editor trimmed the spaces after the 12345 replaced with underscores

结果是“12345_ __” - 抱歉,编辑器在将 12345 替换为下划线后修剪了空格

see MSDN entry for LSET

请参阅LSET 的 MSDN 条目