|
-
February 23rd, 2009, 03:47 PM
#1
Easy String Delete / Insert
****************** Dedicated To WoF! *********************
String editing in VB is a drag! It involves a mixture of Left, Mid, Right, and Len functions. Errors will creep in if calculations of position result in negative values.
Here I go again!
My little di (short for Deletion & Insertion) will change all that!
You just supply:
* Txt$ (string to process)
* N (number of characters to delete)
* Position (where change will take place)
* Insert (characters to insert)
And di will take care of the details!
If Position is positive, change will take place there (or after the end of Txt$).
If Position is negative, it will be calculated backwards from the end of the string. If the result is less than 1, Position = 1
If Position is omitted altogether, action will take place at the end of Txt$. In such case, Position may be used to pass Insertion (in order to avoid the ugly empty comma!)
Please use this little sub and suggest improvements!
' these lines are for testing purposes
Private Sub Form_Load()
Dim txt$
txt$ = "abcdef": Call di(txt$, 1, 2): MsgBox (txt$)
txt$ = "abcdef": Call di(txt$, 1, 2, "X"): MsgBox (txt$)
txt$ = "abcdef": Call di(txt$, 1, -2, "X"): MsgBox (txt$)
txt$ = "abcdef": Call di(txt$, 1, "X"): MsgBox (txt$)
txt$ = "abcdef": Call di(txt$, 1, , "X"): MsgBox (txt$)
txt$ = "abcdef": Call di(txt$, 1, 100, "X"): MsgBox (txt$)
txt$ = "abcdef": Call di(txt$, 100, 1, "X"): MsgBox (txt$)
End Sub
' String Delete and/or Insert
' ~~~~~~~~~~~~~~~~~~~~~~~~~
Sub di(Txt As String, Optional N As Integer = 0, Optional Position As Variant = "", Optional insert As String = "")
' Calc Position
If IsNumeric(Position) = True Then
If Position = 0 Then Position = (Len(Txt) + 1) - Abs(N)
If Position < 0 Then Position = (Len(Txt) + 1) - Abs(Position)
Else
If Position = Nul$ Then
Position = (Len(Txt) + 1) - Abs(N)
Else
If insert = Nul$ Then insert = Position
Position = (Len(Txt) + 1) - Abs(N)
End If
End If
' Do it
Txt = Left$(Txt, Position - 1) + insert + Mid$(Txt, Position + N)
End Sub
-
February 23rd, 2009, 09:06 PM
#2
Re: Easy String Delete / Insert
as i understand you made a function to replace the the character or characters on a signed position.
i can give you some tips to make your code better may be it wont impact performance since its very simple but its nice you get used to think in performance even in simple task like this i mean simple for the PC.
well 1st of all you need to make your conditions easier to look spacing the lines incide of it and leting the if else and end if aligned like this..
Code:
Sub di(Txt As String, Optional N As Integer = 0, Optional Position As Variant = "", Optional insert As String = "")
' Calc Position
If IsNumeric(Position) = True Then
If Position = 0 Then Position = (Len(Txt) + 1) - Abs(N)
If Position < 0 Then Position = (Len(Txt) + 1) - Abs(Position)
Else
If Position = Nul$ Then
Position = (Len(Txt) + 1) - Abs(N)
Else
If insert = Nul$ Then insert = Position
Position = (Len(Txt) + 1) - Abs(N)
End If
End If
' Do it
Txt = Left$(Txt, Position - 1) + insert + Mid$(Txt, Position + N)
End Sub
its kinda hard to see whats going on since you have some lines with IF that execute on the same line.
also use the & for strings instead of + it will be a bit faster.
never do declarations in this way Dim txt$ cause it will be declared as variant declare it in this way Dim txt$ as string, you should avoid em when possible, since you workign with string no need to be a variant.
and would you be more spesific when you say errors will creep when using negative values would you give a example?
and in order to remove that ugly variant on the parameters you may put another parameter for example...
Code:
Sub di(Txt As String, Optional N As Integer = 0, Optional Position As integer,Optional Reverse as boolean=false, Optional insert As String = "")
you see theres no need to use a variant there also the conversions from numeric values to string and vice verse, are pretty slow. and it does look ugly when you use a string on a numeric parameter as position.
this are my recomendations to a better coding.
hoppe it helps cya and happy coding.
Last edited by Alphadan; February 23rd, 2009 at 09:10 PM.
-
February 23rd, 2009, 11:11 PM
#3
Re: Easy String Delete / Insert
Code:
never do declarations in this way Dim txt$ cause it will be declared as variant declare it in this way Dim txt$ as string, you should avoid em when possible, since you workign with string no need to be a variant.
Wow. Find anywhere else that says that, please. (You won't)
There have been short-cuts since the original versions of BASIC that used the same characters. Try this!
Code:
Option Explicit
Private Sub Form_Load()
' there are NO variants below
Dim var1%, var2&, var3#, var4!, var5@, var6$, var7, var8 As Object
Debug.Print "var1: " & TypeName(var1)
Debug.Print "var2: " & TypeName(var2)
Debug.Print "var3: " & TypeName(var3)
Debug.Print "var4: " & TypeName(var4)
Debug.Print "var5: " & TypeName(var5)
Debug.Print "var6: " & TypeName(var6)
Debug.Print "var7: " & TypeName(var7)
Debug.Print "var8: " & TypeName(var8)
' var1: Integer
' var2: Long
' var3: Double
' var4: Single
' var5: Currency
' var6: String
' var7: Empty
' var8: Nothing
End Sub
If I could see some imput, and then desired output, then I'd be able to help implement it
-
February 24th, 2009, 06:50 AM
#4
Re: Easy String Delete / Insert
 Originally Posted by dglienna
Wow. Find anywhere else that says that, please. (You won't)
no need to be rude. i didnt know those shorcuts existed and i just wanted to help...
-
February 24th, 2009, 07:53 AM
#5
Re: Easy String Delete / Insert
Thank you for the suggestions
I pasted the code with spacing, but it was lost at posting. I am new to the site and will try to use the blue code box
Error example:
textA="ABC" : textB=Right(textA, 10)
The point is that you need to make sure that textA is at least 10-char long.
As for the ugly comma, I want syntax like this:
call di (txt, 2, 1)
call di (txt, 2, "X")
You see, 3rd parameter should accept either number or string. This is what Variant is all about.
I did not quite get the "Reverse" suggestion.
Best regards to you all.
Last edited by Bruin 1953; February 24th, 2009 at 08:03 AM.
-
February 24th, 2009, 09:12 AM
#6
Re: Easy String Delete / Insert
To make an error-free Right$() function:
Code:
Function efRight$(StringValue as String, Length as Integer) as String
efRight$ = Right$(Stringvalue, Iif(Length > Len(StringValue), Len(StringValue), Length))
End Function
This makes use of an Iif() expression which returns the minimum of the required length and the actual string length.
If Length > Len(StringVar) then Len(StringVar) is substituted to avoid the error.
@Alphadan: Nobody's being rude.
Your statement was simply wrong, and David was simply a little sarcastic.
Last edited by WoF; February 24th, 2009 at 09:15 AM.
-
February 26th, 2009, 09:41 AM
#7
Re: Easy String Delete / Insert
Hmm.. left, right, mid and len functions are all good but what's wrong with the replace function? It handles in a single line many instances that the sub seems to be intended for. Also looks to me that while you will not end up with a negative value position using the sub above it is very possible that the insert could occur at the wrong location.
On another note would it not be better to create this as a function that returns a value rather than modifiing the original variable?
-
February 26th, 2009, 10:07 AM
#8
Re: Easy String Delete / Insert
To make proper use of the Replace() function you need to know the string within a string which is to be replaced, and then all occurrences of this string are replaced at once.
The function Bruin had written inserts or deletes strings at/from a dedicated position, which works completely different.
The proposal to make it a function which returns the modified string, though, seems not a bad idea if you might make further comparisons between original and modified string.
-
February 26th, 2009, 01:21 PM
#9
Re: Easy String Delete / Insert
True replace does replace text with new text guess I wasn't thinking clearly , Mid() is normally used to insert or delete as is the selstart, sellen and seltext properties of the text control.
Last edited by DataMiser; February 26th, 2009 at 01:23 PM.
-
February 26th, 2009, 05:22 PM
#10
Re: Easy String Delete / Insert
Right. And Mid$() is also no solution, since it does not insert new text between existing characters, but overwrites existing characters instead:
Code:
a$="12345678"
Mid$(a$, 3) = "ABCD"
will leave you wit a$ being "12ABCD78"
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
|