Solved

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

Posted on 2013-02-06
4
457 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 48

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

626 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