Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-02-06
4
Medium Priority
?
464 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:sir plus
[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 2000 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"
1
 
LVL 49

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:sir plus
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

688 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