更改部分网址 - 将用户替换为%USERNAME%

时间:2017-05-21 10:12:53

标签: vb.net

我有一个字符串,可能是:C:\ Users \ info \ Desktop \ Folder,在这种情况下,我想在\ Users \ .. \之后用%Username%

到目前为止,当我知道Username之前的部分时,我设法替换字符串,在本例中是Users \和用户名之后的部分。但我无法定义\作为结束,因为它认为Users \是要替换的字符串的结尾。

那么我怎么能改变昏暗的SDelimEnd所以只返回用户名

Public Class Form1
Private Sub RadButton_Click(sender As Object, e As EventArgs) Handles RadButton.Click
Dim sSource As String = txt1.text 'String that is being searched
Dim sDelimStart As String = "Users" 'First delimiting word
Dim sDelimEnd As String = "AppData" 'Second delimiting word
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2

If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
    Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
    MessageBox.Show(res) 'Display
        Strings.Replace(sSource,res,"\%username%\")
        MessageBox.Show(Strings.Replace(sSource,res,"\%username%\"))
        txt2.Text = Strings.Replace(sSource,res,"\%username%\")

Else
    MessageBox.Show("One or both of the delimiting words were not found!")
End If

End Sub

结束班

3 个答案:

答案 0 :(得分:1)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If System.IO.Directory.Exists("C:\Users\info\" + TextBox1.Text) = True Then
        Label1.Text = "Ok"
    Else
        Label1.Text = "No"
    End If
End Sub

答案 1 :(得分:0)

使用 myurl 声明您的网址并应用此代码:

If myurl.contains("Users") Then
    myurl = myurl.Replace("info\Desktop\Folder", "%Username%")
End If

如果 myurl 可以是更长的路径(或者不同于 info \ Desktop \ Folder ,例如: info \ Desktop \ Folder \ hello \ wazapp )然后:

If myurl.contains("Users") Then
    Dim cut_at As String = "\Users\"
    Dim stringSeparators() As String = {cut_at}
    Dim split = original.Split(stringSeparators, 2, StringSplitOptions.RemoveEmptyEntries)
    myurl = myurl.replace(split(1), "")
End If

答案 2 :(得分:0)

我找到了解决方案,但我无法将Users \定义为Start,将\ n定义为end。

Public Class Form1
Private Sub RadButton_Click(sender As Object, e As EventArgs) Handles RadButton.Click
Dim sSource As String = txt1.text 'String that is being searched
Dim sDelimStart As String = "Users" 'First delimiting word
Dim sDelimEnd As String = "AppData" 'Second delimiting word
Dim nIndexStart As Integer = sSource.IndexOf(sDelimStart) 'Find the first occurrence of f1
Dim nIndexEnd As Integer = sSource.IndexOf(sDelimEnd) 'Find the first occurrence of f2

If nIndexStart > -1 AndAlso nIndexEnd > -1 Then '-1 means the word was not found.
    Dim res As String = Strings.Mid(sSource, nIndexStart + sDelimStart.Length + 1, nIndexEnd - nIndexStart - sDelimStart.Length) 'Crop the text between
    MessageBox.Show(res) 'Display
        Strings.Replace(sSource,res,"\%username%\")
        MessageBox.Show(Strings.Replace(sSource,res,"\%username%\"))


Else
    MessageBox.Show("One or both of the delimiting words were not found!")
End If

End Sub

结束班