Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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
 
#//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 11-Oct-12 16:34pm
Comments
Wes Aday at 11-Oct-12 22:48pm
   
Use split or a regular expression
Sergey Alexandrovich Kryukov at 11-Oct-12 23:05pm
   
I answered using split, to avoid hard-coding often done in Regex expressions -- please see.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It's this:
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. Smile | :)
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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!
Smile | :)
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 6,649
1 Sergey Alexandrovich Kryukov 6,270
2 CPallini 5,190
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 12 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100