Delete some character from text

Delete some character from text

Anonymous
Not applicable
2,611 Views
11 Replies
Message 1 of 12

Delete some character from text

Anonymous
Not applicable

Hi.

I want to delete a character from text objects. 

For example, change "5f10 l=520..." to "5f10 l=520"

This will be a part of another routine so find&replace not usefull for me.

Thank you for your help.

 

0 Likes
Accepted solutions (1)
2,612 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable
<<Delete some character from text

Hi.

I want to delete a character from text objects. 

For example, change "5f10 l=520..." to "5f10 l=520"

This will be a part of another routine so find&replace not usefull for me.

Thank you for your help.>>

Hello, 
I see different solutions:
1) if you know exactly length you want:

ch1 = "5f10 l=520"                      'length you want
ch2 = "7T20 l=520..."                   'What you have ...
LSet ch1 = ch2                             'ch1 takes from ch2 the part equal to his length
result ch1 = "7T20 l=520"        ' replaces ch2 in ch1, keeping it's length
with a loop you can
 replace with different ch1.

2) the classic CUT!    left(var, length)

ch1 = "5f10 l=520" is '10' characters long.
so:
ch2 = left("5f10 l=520...", 10) 
gives
ch2 = "5f10 l=520"

Message 3 of 12

Anonymous
Not applicable

Hi, thank you for your help.

I have very poor knowledge about vba and sometimes i dont understand how to use a command.

I just try how it works. I didnt find a way to use your suggestion.

 

In my drawing there are a lot of texts that contain unwanted characters.

Those texts have different lenght and string.

All texts have one or more "y" at the end, like "5f12 l=350yyyy" or "5f10/10 l=200y".

 

Message 4 of 12

Anonymous
Not applicable

oh I see that length is different from one to the other....
One way can be checking after the " = ".
For example if all the numbers you want after have 3 digits like " = 320"
you can cut counting 3  + space = 4 characters after "=".
using InStr and Mid.
example:
1) looking for "="
ch1 = "5f12 l=350yyyy"          'string
pos = InStr(1, ch1, "=")          'position of "=" in string , here 6
Mid cuts where you want, here I will use from beginning up to "=" + 3.
all together gives the solution you can use:

ch1 = "5f12 l=350yyyy" 
ch2 = Mid(ch1, 1, InStr(1, ch1, "=")+3)          ' gives ch2 = "5f12 l=350"      referring to "=" + 3 characters for 320

2) looking for the first "y" and cutting there:
ch1 = "5f12 l=350yyyy" 
ch2 = Mid(ch1, 1, InStr(1, ch1, "y") -1)           ' gives ch2 = "5f12 l=350"         -1  gives position before "y"

I guess you will use solution 2, involving you know what to clean....
You can trim first the ch1 to clean spaces before and after characters.
Good luck 😉

 

0 Likes
Message 5 of 12

Anonymous
Not applicable

You can also make first a control of the string.
If you want to check for "y" :
For i = 1 to Len(ch1)                       'length of ch1

        if InStr(1, "y", LCase(Mid(ch1, i, 1))) <> 0 Then

                'write down the function

Next i

0 Likes
Message 6 of 12

Anonymous
Not applicable

I am sorry for taking your time.

I just know simple methods end functions to use.

I tried something like this gave an error.

 

For Each ent In ThisDrawing.ModelSpace

  If TypeOf ent Is AcadText Then
  ch1 = CStr(ent) 'This gives error

       For i = 1 To Len(ch1) 'length of ch1

            If InStr(1, "ÿ", LCase(Mid(ch1, i, 1))) <> 0 Then 'write down the function
                 ch2 = Mid(ch1, 1, InStr(1, ch1, "y") - 1) 'do you mean this as the function
            End If

        Next i
End If
Next

0 Likes
Message 7 of 12

Anonymous
Not applicable
 

Try this , tell me if it works.  I did it fast, check it.

 


Sub ConvertText ()

Dim count As Integer
count = ThisDrawing.ModelSpace.count
ReDim newObjs(count) As AcadEntity    'check this
Dim index As Integer , i as integer

Dim ch1 as String, ch2 as String
For index = 0 To count - 1
    Set newObjs(index) = ThisDrawing.ModelSpace.Item(index)

    For Each newObjs In ThisDrawing.ModelSpace

          If TypeOf newObjs Is AcadText Then    'check if your treated text is all AcadText, other do exist.
                ch1 = newObjs .FieldCode  'This gives error  _ AcadText in an object( not a text. his text attribute is FieldCode

                For i = 1 To Len(ch1) 

                        If InStr(1, "y", LCase(Mid(ch1, i, 1))) Then     'write down the function _sorry "<> 0 " not needed, 
                               ch2 = Mid(ch1, 1, InStr(1, ch1, "y") - 1)     'do you mean this as the function _yes two Vb functions.
                               newObjs .FieldCode = ch2
                        End If

                 Next i
         End If
    Next ent 
End Sub

0 Likes
Message 8 of 12

Anonymous
Not applicable

Thank you so much for your help.

One step left i think. 

Code finds the texts, corrects it but doesnt write the new value.

Program gives error as in picture. 

Here ch2 is the corrected text.

ConvertText.JPG

Message 9 of 12

Anonymous
Not applicable

Yes, Indeed. I see . As I had not much time yesterday I didn't check if it was the right method "Field". I guess it's only to ask the field. Try to find the change text method on your side ; I'll see if I find it. Must be easy. 😉

Message 10 of 12

Anonymous
Not applicable

Thank you so much, finally done.

"newObjs(index).TextString = ch2" works fine.

I will try to do faster after now.

Message 11 of 12

Anonymous
Not applicable
Accepted solution

after checking I found the textString method. Works for read and write.
so the line would become:
nexObs(index).TextString = ch2


Try this, tell me me if it works 😉

0 Likes
Message 12 of 12

Anonymous
Not applicable

We found at same time. good work. 🙂
Have good luck for next work.