Results 1 to 12 of 12

Thread: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.2727

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2019
    Posts
    6

    How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.2727

    5362.3426.34274.2343
    to
    5362.3426342742343

    563.36.3
    to
    563.363

    No, rounding off, no "math".
    All I want to do is keep the FIRST decimal dot and remove every other dot that comes after it.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    You could use instr() to locate the first . then use replace() on the rest to remove any and all additional .s
    Or you could use Split() on the . then combine the resulting array into a new string with only the one .

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2019
    Posts
    6

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    Quote Originally Posted by DataMiser View Post
    You could use instr() to locate the first . then use replace() on the rest to remove any and all additional .s
    Or you could use Split() on the . then combine the resulting array into a new string with only the one .
    First of all, thank you.
    Second of all, I created this function on my own freetime in JavaScript back then:
    Code:
    function remove_all_occurrence_of_decimal_dots_except_for_the_first_one(arg)
    {
    	var RawString = arg;
    	var FirstInstanceOfDecimal = RawString.indexOf(".");
    	if (FirstInstanceOfDecimal == -1)
    	{
    		return arg;
    	}
    	else
    	{
    		RawString = RawString.replace(/[.]/g,"");
    		return (RawString.substr(0,FirstInstanceOfDecimal) + "." + RawString.substring(FirstInstanceOfDecimal));
    	}
    }
    I am going to have to find some time to translate that into VB6....if anyone of you have the expertise to translate my code from JavaScript, I would appreciate it.

  4. #4
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,834

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    I slapped this together right before I went home

    Tested just once:

    Code:
    Private Sub Command_Click()
    
        Dim MyString() As String
        MyString() = Split("5362.3426.34274.2343", ".")
        Dim MyResult As String
        
        For i = 0 To UBound(MyString())
            If i = 0 Then
                MyResult = MyString(i) & "."
            Else
                MyResult = MyResult & MyString(i)
            End If
        Next
    
        MsgBox MyResult
        
    End Sub
    Please remember next time...elections matter!

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2019
    Posts
    6

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    I look forward to trying it when I am home @TysonLPrice !

  6. #6
    Lively Member
    Join Date
    May 2017
    Posts
    81

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    Simpler?

    Code:
    Dim s as string
    s = "168.1.0.255"
    Debug.Print Left(s, InStr(s, ".")) & Replace$(s, ".", "", InStr(s, "."))
    More fully:

    Code:
    Function DropExtraDots(strIn As String) As String
    
    If InStr(strIn, ".") > 0 Then
       DropExtraDots = Left(strIn, InStr(strIn, ".")) & Replace$(strIn, ".", "", InStr(strIn, "."))
    Else
       DropExtraDots = strIn
    End If
    
    End Function
    Last edited by MikeSW17; Nov 4th, 2019 at 05:04 PM.

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,852

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    Mike, try s = "123456" in your simple function. You've got to do Instr(s, ".")+1 in the second Instr.


    Code:
    
    Option Explicit
    
    Private Sub Form_Load()
        Dim s As String
        s = "123.456.789":  remove_all_occurrence_of_decimal_dots_except_for_the_first_one s: Debug.Print s ' Prints "123.456789"
        s = "123.456":      remove_all_occurrence_of_decimal_dots_except_for_the_first_one s: Debug.Print s ' Prints "123.456"
        s = "123456":       remove_all_occurrence_of_decimal_dots_except_for_the_first_one s: Debug.Print s ' Prints "123456"
        s = "...876...":    remove_all_occurrence_of_decimal_dots_except_for_the_first_one s: Debug.Print s ' Prints ".876"
    End Sub
    
    Sub remove_all_occurrence_of_decimal_dots_except_for_the_first_one(ByRef arg As String)
        arg = Left$(arg, InStr(arg, ".")) & Replace$(arg, ".", vbNullString, InStr(arg, ".") + 1&)
    End Sub
    
    
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    Quote Originally Posted by GingerSoul View Post
    First of all, thank you.
    Second of all, I created this function on my own freetime in JavaScript back then:
    Code:
    function remove_all_occurrence_of_decimal_dots_except_for_the_first_one(arg)
    {
    	var RawString = arg;
    	var FirstInstanceOfDecimal = RawString.indexOf(".");
    	if (FirstInstanceOfDecimal == -1)
    	{
    		return arg;
    	}
    	else
    	{
    		RawString = RawString.replace(/[.]/g,"");
    		return (RawString.substr(0,FirstInstanceOfDecimal) + "." + RawString.substring(FirstInstanceOfDecimal));
    	}
    }
    ...if anyone of you have the expertise to translate my code from JavaScript, I would appreciate it.
    FYI. VB translation of above:
    Code:
    function remove_all_occurrence_of_decimal_dots_except_for_the_first_one(arg As String) As String
        Dim RawString As String
        Dim FirstInstanceOfDecimal As Long
    
        RawString = arg
        FirstInstanceOfDecimal = InStr(RawString, ".", "")
        If FirstInstanceOfDecimal = 0 Then
            remove_all_occurrence_of_decimal_dots_except_for_the_first_one = arg
        Else
            RawString = Replace(RawString, ".")
            RawString = Left$(RawString, FirstInstanceOfDecimal - 1) & "." & Mid$(RawString, FirstInstanceOfDecimal)
            remove_all_occurrence_of_decimal_dots_except_for_the_first_one = RawString
        End If
    End Function
    Above provided just because I was bored and you asked
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    Lots of possibilities:

    Code:
    Option Explicit
    
    Private Function OneDot(ByVal Text As String) As String
        Dim Parts() As String
    
        Parts = Split(Text, ".")
        If UBound(Parts) > 0 Then Parts(0) = Parts(0) & "."
        OneDot = Join$(Parts, vbNullString)
    End Function
    
    Private Sub Form_Load()
        Debug.Print "1.2.3.4", OneDot("1.2.3.4")
        Debug.Print "123.456.789", OneDot("123.456.789")
        Debug.Print "123.456", OneDot("123.456")
        Debug.Print "123456", OneDot("123456")
    End Sub
    Code:
    1.2.3.4       1.234
    123.456.789   123.456789
    123.456       123.456
    123456        123456

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2019
    Posts
    6

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    Quote Originally Posted by LaVolpe View Post
    FYI. VB translation of above:
    Code:
    function remove_all_occurrence_of_decimal_dots_except_for_the_first_one(arg As String) As String
        Dim RawString As String
        Dim FirstInstanceOfDecimal As Long
    
        RawString = arg
        FirstInstanceOfDecimal = InStr(RawString, ".", "")
        If FirstInstanceOfDecimal = 0 Then
            remove_all_occurrence_of_decimal_dots_except_for_the_first_one = arg
        Else
            RawString = Replace(RawString, ".")
            RawString = Left$(RawString, FirstInstanceOfDecimal - 1) & "." & Mid$(RawString, FirstInstanceOfDecimal)
            remove_all_occurrence_of_decimal_dots_except_for_the_first_one = RawString
        End If
    End Function
    Above provided just because I was bored and you asked
    VB6 is screaming, any advise ?
    Code:
    Function remove_all_occurrence_of_decimal_dots_except_for_the_first_one(arg As String) As String
        Dim RawString As String
        Dim FirstInstanceOfDecimal As Long
    
        RawString = arg
        FirstInstanceOfDecimal = InStr(RawString, ".", "")
        If FirstInstanceOfDecimal = 0 Then
            remove_all_occurrence_of_decimal_dots_except_for_the_first_one = arg
        Else
            RawString = Replace(RawString, ".")
            RawString = Left$(RawString, FirstInstanceOfDecimal - 1) & "." & Mid$(RawString, FirstInstanceOfDecimal)
            remove_all_occurrence_of_decimal_dots_except_for_the_first_one = RawString
        End If
    End Function
    
    Private Sub Command1_Click()
        Print remove_all_occurrence_of_decimal_dots_except_for_the_first_one("562.1.1.1.1.1.1.1")
    End Sub
    Compile error:
    Argument not optional

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2019
    Posts
    6

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    @MikeSW17
    Your code works PERFECTLY !!!

  12. #12
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.

    Quote Originally Posted by GingerSoul View Post
    VB6 is screaming, any advise ?
    Code:
    Function remove_all_occurrence_of_decimal_dots_except_for_the_first_one(arg As String) As String
        Dim RawString As String
        Dim FirstInstanceOfDecimal As Long
    
        RawString = arg
        FirstInstanceOfDecimal = InStr(RawString, ".", "")
        If FirstInstanceOfDecimal = 0 Then
            remove_all_occurrence_of_decimal_dots_except_for_the_first_one = arg
        Else
            RawString = Replace(RawString, ".")
            RawString = Left$(RawString, FirstInstanceOfDecimal - 1) & "." & Mid$(RawString, FirstInstanceOfDecimal)
            remove_all_occurrence_of_decimal_dots_except_for_the_first_one = RawString
        End If
    End Function
    
    Private Sub Command1_Click()
        Print remove_all_occurrence_of_decimal_dots_except_for_the_first_one("562.1.1.1.1.1.1.1")
    End Sub
    Compile error:
    Argument not optional
    Typo. Should be: RawString = Replace(RawString, ".", ""). Was missing last parameter.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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