本文介绍了如何在vb.net中声明一个固定长度的字符串而不在赋值过程中增加长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a fixed length string variable:

<VBFixedString(10)>
Public myVar As String

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

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

这是我得到的:

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

我想要的是 myvar 是:1234567890"

What I want is myvar to be: "1234567890"

然后如果它小于 10 我想要

and then if it is less than 10 I would like

myvar = "12345" to be "12345     "

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

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.

推荐答案

和老派一样,LSET 将完全满足您的要求.

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

 myvar = lset("1234567890ABCD",10)

结果为1234567890",而

results in "1234567890", while

 myvar = lset("12345",10)

结果为12345___" - 抱歉,编辑器在将 12345 替换为下划线后修剪了空格

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

请参阅 LSET 的 MSDN 条目

这篇关于如何在vb.net中声明一个固定长度的字符串而不在赋值过程中增加长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:01