奇怪的字符串操作问题

时间:2016-07-07 10:54:05

标签: vb.net

我试图使一系列字符串的长度完全相同。我正在尝试这些步骤:

  1. 如果字符串超过25个字符,请修剪额外的字符。
  2. 如果小于25,请用空格填充。
  3. 确保字符串长度均为25个字符。
  4. 这是我的代码(或至少是相关位)

    If Len(My.Settings.currOrder1) > 25 Then
        My.Settings.currOrder1 = Microsoft.VisualBasic.Left(My.Settings.currOrder1, 25)
    Else
        My.Settings.currOrder1 = My.Settings.currOrder1 + "                           "
        My.Settings.currOrder1 = Microsoft.VisualBasic.Left(My.Settings.currOrder1, 25)
    End If
    
    在我的头脑中应该工作。但是,如果我使用6个字符输入运行该代码3次,然后7,然后8,我得到的输出是这样的:

    enter image description here

    这显然不会缩短任何东西(价格只是在最后显示,以显示字符串结束的位置)。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

Dim str As String = "Abc"
If str.Length < 25 Then
  str = str.PadRight(25 - str.Length, " "C)
Else
  str = str.Substring(0, 25)
End If

答案 1 :(得分:0)

一般来说,你的VB6方法也应该有用,但是我使用.NET:

warning IKVMC0108: Not a class file "Counters/AtomicCounter.class", including it as resource  (class format error "52.0")
warning IKVMC0108: Not a class file "Counters/CharCounter.class", including it as resource (class format error "52.0")
warning IKVMC0108: Not a class file "Counters/Thread.class", including it as resource (class format error "52.0")
warning IKVMC0108: Not a class file "File/FileController.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame$1.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame$10.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame$2.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame$3.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame$4.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame$5.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame$6.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame$7.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame$8.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame$9.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Gui/MainFrame.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "ListElement/ListObject.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Performance/RamSample.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Performance/RamSpectator.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "Performance/TimeKeeper.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "threadtester/ThreadTester$1.class", including it as resource(class format error "52.0")
warning IKVMC0108: Not a class file "threadtester/ThreadTester.class", including it as resource(class format error "52.0")
note IKVMC0004: Using main class "threadtester.ThreadTester" based on jar manifest
note IKVMC0002: Output file is "ThreadTester.exe"
fatal error IKVMC5033: Main class not found