0

Having some issues in vb.net string to join two strings together for output to a txt document. My text document is getting an extra space between the two strings I join. This should not be happening.

           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

THIS IS THE PART OF THE CODE THAT IS ADDING EXTRA SPACES

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

Sample output: Dim hybridShapePointOnCurve 2

SHOULD be: Dim hybridShapePointOnCurve2

What modifications should I make to the way I join strings?

Thanks!

Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • 1
    `count.Replace(" ", "")` isn't doing what you think it is. The function returns a string - so you would need to do `count = count.Replace(" ", "")` for it to work correctly. – entropic Jul 10 '14 at 02:29
  • Just swapped that out, still not working. I only added that as a last resort anyways. str(i) shouldn't give any spaces to begin with. – Peter Beamish Jul 10 '14 at 02:34
  • You can use the `trim()` method to trim empty spaces instead. So instead of `count.Replace(" ", "")` you can do `count = trim(count)`. Same for your `lessOneCount = "HybridShapePointOnCurve" + Str(i - 1)` use `lessOneCount = "HybridShapePointOnCurve" + trim(Str(i - 1))` – Sky Jul 10 '14 at 02:36
  • trim worked for some of the strings. This is still giving an extra space. lessOneCount = Trim(lessOneCount) sw.WriteLine("reference2 = part1.CreateReferenceFromObject(" + lessOneCount + ")") – Peter Beamish Jul 10 '14 at 02:49
  • You need to use your debugger to step through your program looking at your variables to determine where your errant space is coming from, VB does not pull them out of clear air. – Mark Hall Jul 10 '14 at 02:50
  • @PeterBeamish you're suppose to trim the `Str(i - 1)` to be `trim(Str(i-1))` and not the `lessOneCount` variable. trim doesn't trim the spaces in between the line, it only trim the starting and ending of the Strings if it's spaces, asterisk or apostrophe. Trimming `lessOneCount` is no use as when the String is being Concat, the spaces is in between the characters which result it not being trimed – Sky Jul 10 '14 at 02:55

2 Answers2

0

Realized my comment didn't answer your question completely, so a few things:

  1. The String.Replace method doesn't work like you think it does. Saying count.Replace(" ", "") doesn't actually change the variable count; the function actually returns a new string. For it work like you want, you would need to do count = count.Replace(" ", "").

  2. Consider using a StringBuilder instead of concatenating a bunch of strings together. It will greatly improve performance. When using the StringBuilder class, you also wouldn't need to write to the StreamWriter until the very end, simply by calling sw.Write(stringBuilderObject.ToString())

  3. I haven't used VB.NET in a while, but I would check what Str(i) returns. Instead of using VB functions, consider using .NET functions by saying count = i.ToString(). According to Sky, the Str function indeed returns a space because it reserves it for the negative sign. Based on this, I would use the .ToString() method instead.

entropic
  • 1,683
  • 14
  • 27
  • The issue with ops is actually Str(i), it will return " 1", " 2" and so. There is a space because it's reserved for a negative sign. That's what the method is returning. He could just trim the spaces or use ToString method as you suggested – Sky Jul 10 '14 at 02:49
  • If that's the case, then he should definitely use `i.ToString()` and `(i - 1).ToString()` – entropic Jul 10 '14 at 02:53
  • Yes, if he just want to use `Str` function then he will need to trim the empty spaces in that case. – Sky Jul 10 '14 at 03:01
0

I spoke to quickly in my comment, the Str function reserves room for the sign, since it is a positive number there is no sign, if it was negative there would be a minus sign there. Try Trim(Str(i)) instead

From above link(emphasis mine):

This example uses the Str function to return a String representation of a number. When a positive number is converted to a string, a leading space is always reserved for its sign.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111