Click here to Skip to main content
15,891,431 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!.

VB
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

#//ASUS-PC/Users/Username/Desktop/Rays
!//DELL-PC/Users/Username/Desktop/Advan


So my output is string "a" is
ASUS-PC
DELL-PC

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.
Posted
Comments
[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.
--SA

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


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

Please see:
http://msdn.microsoft.com/en-us/library/system.io.path.pathseparator.aspx[^],
http://msdn.microsoft.com/en-us/library/system.string.split.aspx[^],
http://msdn.microsoft.com/en-us/library/ms131448.aspx[^],
http://msdn.microsoft.com/en-us/library/system.stringsplitoptions.aspx[^].

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

—SA
 
Share this answer
 
try this,
VB
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
 
v2

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