thansen
thansen

Reputation: 1

How to concatenate the data on one line instead of two?

I spend a lot of time at the office manually combining latitudes and longitudes to paste into a program that locates our customers and sets up for repairs.

As an example... I copy this from the customers information page:

43.481075
CPE LON
-84.787613

I then manually modify it to look like:

43.481075 -84.787613

I'm attempting to come up with some code that will do this for me. the problem I'm encountering is, no matter what I try the latitude and longitude always end up on separate lines

43.481075
-84.787613

I've tried removing vblf,vbcrlf and vbnewline before combining the strings "lat" and "lon". I've tried using lat & lon, lat + lon, latlon = String.Concat(lat, " ", lon) and in my last attempt, I used stringbuilder to try and concatenate. It always gives both on separate lines. What am I missing?

Here's my latest version:

Imports System.Text.RegularExpressions
Imports System.Text

Public Class Form1
    Public c As Integer
    Public lastlat As Integer

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Copy string to format from clipboard
        Dim strClipText As String = Clipboard.GetText

        ' Remove CPE LON from String
        Dim clean As String = strClipText.Replace("CPE LON", "")
        strClipText = clean

        strClipText = Replace(strClipText, vbLf, "")
        strClipText = Replace(strClipText, vbCrLf, "")
        strClipText = Replace(strClipText, vbNewLine, "")

        'Get string length
        Dim length As Integer = strClipText.Length

        ' Find minus sign (Start of Longitude)
        For c1 = 1 To length - 1
            Dim cchk As Char = strClipText(c1)
            If cchk = "-" Then
                c = c1
                lastlat = c1 - 1
            End If
        Next

        Dim lastc As Integer = length - 1
        Dim cchk1 As String = strClipText(lastc)
        Dim lat As String = strClipText.Substring(0, lastlat)
        Dim lon As String
        Dim lon1 As String
        For curc = c To lastc
            lon1 = strClipText(c)
            lon = lon & lon1
            c = c + 1
        Next

        Dim builder As StringBuilder = New StringBuilder(lat)
        Dim llen As Integer = lat.Length
        builder.Insert(llen, lon)
        Dim latlon As String = builder.ToString()

        Clipboard.Clear()
        My.Computer.Clipboard.SetText(latlon)
    End Sub
End Class

Upvotes: 0

Views: 42

Answers (2)

Enigmativity
Enigmativity

Reputation: 117175

It sounds like you're doing a lot of work manipulating the string that's causing you bugs. I think it would be easier to go with Regex to solve this.

Try this code:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ' Copy string to format from clipboard
    Dim strClipText As String = Clipboard.GetText
    Dim regex = New Regex("-{0,1}\d+(\.\d+|)")
    Dim latlon = String.Join(" ", regex.Matches(strClipText).OfType(Of Match)().Select(Function(m) m.Value))
    Clipboard.Clear()
    My.Computer.Clipboard.SetText(latlon)
End Sub

Upvotes: 1

Stephen Wrighton
Stephen Wrighton

Reputation: 37880

Please notice that you're never replacing the carriage return.

Don't forget that these constants turn into actual ASCII characters or combinations of ASCII characters

  • vbCr == Chr(13)
  • vbLf == Chr(10)
  • vbCrLf == Chr(13) + Char(10)
  • vbNewLine == Chr(13) + Char(10)

Now, in your code, you're doing this:

strClipText = Replace(strClipText, vbLf, "")
strClipText = Replace(strClipText, vbCrLf, "")
strClipText = Replace(strClipText, vbNewLine, "")

Which does these three things:

  1. Replace Chr(10) with an empty string
  2. Replace Chr(13)+Char(10) with an empty string
  3. Replace Chr(13)+Char(10) with an empty string

Thus, you're never getting rid of the Chr(13) which will sometimes show as a new line. Because, even if the lines begin life as Char(13) + Char(10) (vbCrLf) when you replace vbLf with an empty string, you're breaking up the Char(13) + Char(10).

Do something like this instead:

strClipText = Replace(strClipText, vbCrLf, "")
strClipText = Replace(strClipText, vbNewLine, "")
strClipText = Replace(strClipText, vbCR, "")
strClipText = Replace(strClipText, vbLf, "")

Upvotes: 0

Related Questions