0

I have this function: (server is a textbox field) & (domain is a textbox field)

Function foo As Boolean

    System.Web.HttpContext.Current.Session("site") = "server1.example.com"

If(server.Text.ToLower.Contains((System.Web.HttpContext.Current.Session("site").ToString.Substring("."))
 OR
domain.Text.ToLower.Contains((System.Web.HttpContext.Current.Session("site").ToString.Substring(".")))

        Return True
    Else
        Return False
    End If 
End Function

I understand that this will not work because my string split returns a string array, which cannot be compared to the string "server.text". But how could I re-write this comparison so that I can tell if either the textbox Strings contain the session variable? Keeping in mind I don't want to use any type of iteration over an array of string values.

Expected output:

server.Text = server1
domain.Text = domain2
System.Web.HttpContext.Current.Session("site") = "server1.example.com"

foo = True // server1 = server1

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Christopher
  • 790
  • 12
  • 30
  • You can try something like this: http://stackoverflow.com/questions/10735190/parsing-string-for-domain-hostname – Rick S Sep 16 '15 at 19:16
  • So what if I wasn't using a URI resource and still wanted to preform a similar string... maybe if the string was instead usernames formated first.last name? – Christopher Sep 16 '15 at 19:20
  • Why not write your own method that does the looping through the split string and returns the string your looking for? – Rick S Sep 16 '15 at 19:31
  • 1
    ***Turn ON OPTION STRICT*** – Trevor Sep 16 '15 at 19:31
  • Why do you need to split the string at all? Why not just look for the text in the text box and treat the environment variable as a string? – Duston Sep 16 '15 at 19:37

2 Answers2

0

Found my answer in how to take selective parts of a string.split...

ToString.Split(".")(0) ' takes string in array position 0

ToString.Split(".")(1) ' takes string in array position 1

ToString.Split(".")(2) ' takes string in array position 2

so

server.Text.ToLower.Contains((System.Web.HttpContext.Current.Session("site").ToString.Substring("."c)(0)) 

returns true if the first array element is contained in the server string

Christopher
  • 790
  • 12
  • 30
  • Just to note here - you'll get a massive performance overhead if you refer to each of these in turn. You'd be far better assigning the output of `String.Split` to an array: `Dim mySess() As String = System.Web.HttpContext.Current.Session("site").ToString.Split("."C)` – Paul Jan 26 '16 at 10:07
0

SubString won't help you here.

If you want to check whether each URI part is contained in what is in the corresponding textbox, you do it like this:

Function foo() As Boolean
    'System.Web.HttpContext.Current.Session("site") = "server1.domain.com"
    Dim parts() As String = Split(System.Web.HttpContext.Current.Session("site").ToLower, ".")
    If server.Text.ToLower.Contains(parts(0)) OrElse domain.Text.ToLower.Contains(parts(1)) Then
        Return True
    Else
        Return False
    End If
End Function

If you want the parts to match exactly with either of the corresponding textboxes then you check for equality instead of Contains:

Function foo() As Boolean
    'System.Web.HttpContext.Current.Session("site") = "server1.domain.com"
    Dim parts() As String = Split(System.Web.HttpContext.Current.Session("site").ToLower, ".")
    If server.Text.ToLower = parts(0) OrElse domain.Text.ToLower = parts(1) Then
        Return True
    Else
        Return False
    End If
End Function
Pradeep Kumar
  • 6,836
  • 4
  • 21
  • 47