0

在 vb.net 字符串中存在一些问题,将两个字符串连接在一起以输出到 txt 文档。我的文本文档在我加入的两个字符串之间有一个额外的空间。这不应该发生。

           If System.IO.File.Exists(path) = True Then
        ' Create a file to write to. 
        Dim sw As StreamWriter = System.IO.File.CreateText(path)
        sw.WriteLine("Attribute VB_Name = " & Chr(34) & "KbTest" & Chr(34))
        sw.WriteLine("")
        sw.WriteLine("Public Sub DoKbTest()")
        sw.WriteLine("'code here")
        sw.WriteLine("Dim catia")
        sw.WriteLine("set catia = GetObject(, " & Chr(34) & "CATIA.Application" & Chr(34) & ")")
        sw.WriteLine("Dim partDocument1")
        sw.WriteLine("Set partDocument1 = catia.ActiveDocument")
        sw.WriteLine("Dim part1")
        sw.WriteLine("Set part1 = partDocument1.Part")
        sw.WriteLine("Dim hybridShapeFactory1")
        sw.WriteLine("Set hybridShapeFactory1 = part1.HybridShapeFactory")
        sw.WriteLine("Dim hybridShapeDirection1")
        sw.WriteLine("Set hybridShapeDirection1 = hybridShapeFactory1.AddNewDirectionByCoord(1#, 2#, 3#)")
        sw.WriteLine("Dim hybridBodies1")
        sw.WriteLine("Set hybridBodies1 = part1.HybridBodies")
        sw.WriteLine("Dim hybridBody1")
        sw.WriteLine("Set hybridBody1 = hybridBodies1.Item(" & Chr(34) & "PointSetx" & Chr(34) & ")")
        sw.WriteLine("dim skteches1")
        sw.WriteLine("Set sketches1 = hybridBody1.HybridSketches")
        sw.WriteLine("Dim sketch1")
        sw.WriteLine("Set sketch1 = sketches1.Item(" & Chr(34) & "Sketch.1" & Chr(34) & ")")
        sw.WriteLine("Dim reference1")
        sw.WriteLine("Set reference1 = part1.CreateReferenceFromObject(sketch1)")
        sw.WriteLine("Dim hybridShapeExtremum1")
        sw.WriteLine("Set hybridShapeExtremum1 = hybridShapeFactory1.AddNewExtremum(reference1, hybridShapeDirection1, 1)")
        sw.WriteLine("hybridBody1.AppendHybridShape hybridShapeExtremum1")
        sw.WriteLine("Dim reference2")

        Dim lessOneCount As String
        Dim spacing As Double = 0
        Dim count As String
        Dim setString As String
        'loop

这是添加额外空间的代码的一部分

For i = 1 To numbOfPoints Step 1

            count = Str(i)
            count.Replace(" ", "")
            MsgBox(count)
            If i < 2 Then
                lessOneCount = "hybridShapeExtremum1"
            Else
                lessOneCount = "HybridShapePointOnCurve" + Str(i - 1)
            End If

            sw.WriteLine("reference2 = part1.CreateReferenceFromObject(" + lessOneCount + ")")

            setString = "Dim hybridShapePointOnCurve" + count
            sw.WriteLine(setString)
            setString = "hybridShapePointOnCurve" + count + " = hybridShapeFactory1.AddNewPointOnCurveWithReferenceFromDistance(reference1, reference2, spacing, False)"
            sw.WriteLine(setString)
            setString = "hybridShapePointOnCurve" + count + ".DistanceType = 1"
            sw.WriteLine(setString)
            setString = "hybridBody1.AppendHybridShape(hybridShapePointOnCurve" + count + ")"
            sw.WriteLine(setString)


        Next

        sw.WriteLine("End Sub")


        sw.Flush()
        sw.Close()
    End If

示例输出:Dim hybridShapePointOnCurve 2

应该是:Dim hybridShapePointOnCurve2

我应该对加入字符串的方式进行哪些修改?

谢谢!

4

2 回答 2

0

意识到我的评论并没有完全回答你的问题,所以有几件事:

  1. String.Replace方法不像您认为的那样有效。说count.Replace(" ", "")实际上并没有改变变量count;该函数实际上返回一个新字符串。为了让它像你想要的那样工作,你需要做count = count.Replace(" ", "").

  2. 考虑使用 aStringBuilder而不是将一堆字符串连接在一起。它将大大提高性能。使用StringBuilder该类时,您也不需要写到StreamWriter最后,只需调用sw.Write(stringBuilderObject.ToString())

  3. 我有一段时间没有使用 VB.NET,但我会检查Str(i)返回的内容。考虑使用 .NET 函数,而不是使用 VB 函数,说count = i.ToString(). 根据 Sky 的说法,Str 函数确实返回了一个空格,因为它为负号保留了空格。基于此,我将使用该.ToString()方法。

于 2014-07-10T02:42:39.627 回答
0

我在评论中很快谈到,该Str功能为符号保留空间,因为它是一个正数,所以没有符号,如果它是负数,那里会有一个减号。试试Trim(Str(i))

从上面的链接(强调我的):

此示例使用 Str 函数返回数字的字符串表示形式。将正数转换为字符串时,始终为其符号保留前导空格。

于 2014-07-10T03:03:22.053 回答