Solved

Trim(" my string      ") produces " my string      "

Posted on 2013-02-06
4
446 Views
Last Modified: 2013-02-06
I ran into this using vba code but was able to reproduce it in the immediate window

Enter
?">" & trim("SALLY ALLAN                  ") & "<"
in the immediate window then hit enter

Result:
>SALLY ALLAN                  <
I am after >SALLY ALLAN<

Have tried  
?">" & Application.trim("SALLY ALLAN                  ") & "<"
&
?">" & Strings.trim("SALLY ALLAN                  ") & "<"
As well as
?">" & Application.trim(cstr("SALLY ALLAN                  ")) & "<"
&
?">" & Strings.trim(cstr("SALLY ALLAN                  ")) & "<"


All give >SALLY ALLAN                  <
I need >SALLY ALLAN<

Sure I can parse it only this stuff is supposed to be native so what's going on??
Thanks in advance -easy points for someone with a clear mind..........

PS
No librarys are missing and
on error goto 0
is in effect

Also tried in code
   Dim strSearch as string
    strSearch = Replace("SALLY ALLAN                  ", vbCr, " ")
    strSearch = Replace(strSearch, vbLf, " ")
    strSearch = Trim(strSearch)
    debug.print ">" & strSearch & "<"
Still >SALLY ALLAN                  <

Resetting all librarys did not work either
(running C:\Program Files (x86)\Microsoft Office\Office14\Excel.exe   /regserver).
:-/
0
Comment
Question by:sirplus
4 Comments
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 38862711
the string was not ending with sapce character. i just copied the string you posted and checked it's showing unicode 3F character from Latin block. from where did you get the input? if you try typing following statement by your self not copy paste,
?trim("SALLY ALLAN     ") returns you "SALLY ALLAN"
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 38862713
When I do

?">" & trim("SALLY ALLAN                  ") & "<"

I get

>SALLY ALLAN<

Try

?">" & VBA.trim("SALLY ALLAN                  ") & "<"

If that works for you as I suspect it will, go to Tools|References and you'll probably find that one is "Missing".
0
 
LVL 40
ID: 38862721
Are you sure that what looks like spaces are spaces? It could be another ASCII character that does not display. If the thing was copied from Word, then it

Try

? Asc(Mid("SALLY ALLAN                  ",12,1))

The result should be 32, the ASCII code for a space. If not, then you have special character that you will need to replace by " " before trimming the string.
0
 
LVL 5

Author Comment

by:sirplus
ID: 38862776
I jigged up this function to clean it up
Function fStrLettersAndBlanksOnly(ByVal pstrString As String)
    Dim i As Integer
    For i = Len(pstrString) To 1 Step -1
        Select Case Asc(Mid(pstrString, i, 1))
            Case 32 To 126
                        
            Case Else
                pstrString = Left(pstrString, i - 1) & Right(pstrString, Len(pstrString) - i)
            End Select
    Next i
    fStrLettersAndBlanksOnly = pstrString
End Function

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
Introduction This Article is a follow-up to my Mappit! Addin Article (http://www.experts-exchange.com/A_2613.html), it was inspired by an email posting I made to EUSPRIG (http://www.eusprig.org/index.htm), I will briefly cover: 1) An overvie…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now