-
Nov 4th, 2019, 01:49 PM
#1
Thread Starter
New Member
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.
-
Nov 4th, 2019, 02:26 PM
#2
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 .
-
Nov 4th, 2019, 02:37 PM
#3
Thread Starter
New Member
Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.
Originally Posted by DataMiser
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.
-
Nov 4th, 2019, 02:56 PM
#4
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!
-
Nov 4th, 2019, 03:13 PM
#5
Thread Starter
New Member
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 !
-
Nov 4th, 2019, 03:23 PM
#6
Lively Member
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.
-
Nov 4th, 2019, 04:42 PM
#7
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.
-
Nov 4th, 2019, 04:54 PM
#8
Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.
Originally Posted by GingerSoul
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
-
Nov 4th, 2019, 05:06 PM
#9
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
-
Nov 4th, 2019, 11:41 PM
#10
Thread Starter
New Member
Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.
Originally Posted by LaVolpe
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
-
Nov 4th, 2019, 11:52 PM
#11
Thread Starter
New Member
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 !!!
-
Nov 5th, 2019, 06:58 AM
#12
Re: How do you remove extra dots in a string of no. ? Like clean up 524.27.27 to 524.
Originally Posted by GingerSoul
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|