Results 1 to 5 of 5

Thread: [RESOLVED] Replacing everything before/after a certain dot

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2016
    Posts
    54

    Resolved [RESOLVED] Replacing everything before/after a certain dot

    Hello, my tool is collecting the users' IP addresses and I'd like to only collect a part of it not the full one.
    Example: user IP is 100.200.300.400
    I want it to get collected as 100.xxx.xxx.400
    Example 2: user IP is 157.12.9.75
    I want it to get collected as 157.xx.x.75
    But I'm not sure how to replace the middle part since there isn't only 1 dot.
    Thanks in advance!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Replacing everything before/after a certain dot

    You can use stringVariable.Split("."c) to create an array of the parts, then modify the elements you want, and then join the array back together.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Replacing everything before/after a certain dot

    Quote Originally Posted by si_the_geek View Post
    and then join the array back together.
    For the record, the String.Join method will perform the inverse operation, i.e. join an array of substrings into a single String with a delimiter in between each pair.

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Replacing everything before/after a certain dot

    Hi,

    here a few samples

    Code:
     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            'you need to Import = Imports System.Net
            Dim ip As IPAddress = IPAddress.Parse("157.12.9.75")
    
            For Each i As Byte In ip.GetAddressBytes()
    
            Next
            ListBox1.Items.Add(ip.GetAddressBytes(0) & ".xxx.xxx." & ip.GetAddressBytes(3))
        End Sub
        
    
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            Dim ip As String = "157.12.9.75"
            Dim tokens As String() = ip.Split("."c)
            Dim value1 As Integer = Int32.Parse(tokens(0))
            Dim value2 As Integer = Int32.Parse(tokens(1))
            Dim value3 As Integer = Int32.Parse(tokens(2))
            Dim value4 As Integer = Int32.Parse(tokens(3))
            TextBox1.Text = value1 & ".xxx.xxx." & value4
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2016
    Posts
    54

    Re: Replacing everything before/after a certain dot

    Thank you all, that's really helpful!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width