如何在一行而不是两行上连接数据?

时间:2019-03-24 01:44:31

标签: vb.net

我在办公室花费了大量时间,将纬度和经度手动组合在一起,粘贴到一个程序中,该程序可以找到我们的客户并进行维修。

作为示例...我从客户信息页面复制此内容:

43.481075
CPE LON
-84.787613

然后我将其手动修改为:

43.481075 -84.787613

我正在尝试提出一些代码来帮助我。我遇到的问题是,无论我尝试什么,纬度和经度总是以单独的行结尾

43.481075
-84.787613

在组合字符串“ lat”和“ lon”之前,我尝试过删除vblf,vbcrlf和vbnewline。我尝试使用lat&lon,lat + lon,latlon = String.Concat(lat,“”,lon),在上一次尝试中,我使用stringbuilder进行尝试和连接。它总是在单独的行上同时给出两者。我想念什么?

这是我的最新版本:

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

2 个答案:

答案 0 :(得分:1)

听起来您在处理导致错误的字符串方面做了大量工作。我认为使用Regex解决这个问题会更容易。

尝试以下代码:

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

答案 1 :(得分:0)

请注意,您永远不会更换回车。

别忘了这些常量会变成实际的ASCII字符或ASCII字符组合

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

现在,在您的代码中,您正在执行以下操作:

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

这三件事:

  1. 用空字符串替换Chr(10)
  2. 用空字符串替换Chr(13)+ Char(10)
  3. 用空字符串替换Chr(13)+ Char(10)

因此,您永远不会摆脱Chr(13),它有时会显示为新行。因为,即使用空字符串替换vbLf时,即使这些行以Char(13)+ Char(10)(vbCrLf)开始,也要破坏Char(13)+ Char(10)。

改为执行以下操作:

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