Click here to Skip to main content
15,942,710 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I want to extract the first "word" which is actually a host name to parse into another method. I currently hard coded it. Is there anyway to soft code it to take data between "//" & "/". Using VB.NET thanks!.

Dim s As String
Dim a As String
Dim f As String
For Each s In readText
    If s.Trim.StartsWith("#") Then
        SourceTextBox.Text = s.TrimStart("#")
        s = s.TrimStart("#")
        f = s.Substring(2)
        Dim j As Integer = f.IndexOf("/")
        a = f.Substring(0, j)
    End if

    If s.Trim.StartsWith("!") Then
        TargetTextBox.Text = s.TrimStart("!")
        s = s.TrimStart("!")
        f = s.Substring(2)
        Dim j As Integer = f.IndexOf("/")
        a = f.Substring(0, j)

    End If

This is the text file


So my output is string "a" is

But I want to soft-code it for better error checking. Since currently I hard-code it to count from the start of each line.
[no name] 11-Oct-12 22:48pm    
Use split or a regular expression
Sergey Alexandrovich Kryukov 11-Oct-12 23:05pm    
I answered using split, to avoid hard-coding often done in Regex expressions -- please see.

It's this:
static string ExtractComputerName(string fileName) {
   string[] parts = fileName.Split(
      new char[] { System.IO.Path.PathSeparator },
   return parts[0]; 

Pay attention: even "\" was not hard-coded, thanks to System.IO.Path.PathSeparator.

Please see:[^],[^],[^],[^].

Next time, try to explore MSDN more thoroughly, to find all what can help you — it's not rocket surgery. :-)

Share this answer
try this,
Dim Result As string=""
Dim str = "#//ASUS-PC/Users/Username/Desktop/Rays"
Dim doubleSlashInd = str.IndexOf("//") + 2
Dim FirstSingleSlashAfterDoubleInd = str.IndexOf("/", doubleSlashInd)
Result = str.Substring(doubleslashInd, FirstSingleSlashAfterDoubleInd - doubleslashInd)
'result= asus-pc   same way it will give result for dell-pc

Happy Coding!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900