Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to delete the spaces in the string?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str7 code
    "       011592"
    "       004698"
    "       005927"
    "       005928"
    "       001245"
    "       005669"
    "011146" 
    "       011147"
    "       160643"
    "       010364"
    end

    Thanks so much!

  • #2
    why the above code cannnot generate observations? I generated using dataex command.

    Comment


    • #3
      You may try

      Code:
      replace code = subinstr(code, " ", "", .)

      Comment


      • #4
        Originally posted by Fei Wang View Post
        You may try

        Code:
        replace code = subinstr(code, " ", "", .)
        It cannot work. I guess there is some coding(gbk/unicode) issue in the dataset.

        Comment


        • #5
          https://xexx-my.sharepoint.com/:u:/g...1sKcg?e=b3NwCU
          Here is the original sample

          Comment


          • #6
            Originally posted by Fred Lee View Post
            This will work.

            Code:
            replace code = ustrtrim(code)

            Comment


            • #7
              Originally posted by Fei Wang View Post

              This will work.

              Code:
              replace code = ustrtrim(code)
              Thanks! Correct! I tried
              Code:
              replace `v'=usubinstr(`v'," ","",.)
              beforre, why this cannot work? Thanks

              Comment


              • #8
                Originally posted by Fred Lee View Post

                Thanks! Correct! I tried
                Code:
                replace `v'=usubinstr(`v'," ","",.)
                beforre, why this cannot work? Thanks
                I guess it's because the leading space in code is not really "space" but some other invisible characters, so they cannot be explicitly replaced.

                Comment


                • #9
                  Originally posted by Fei Wang View Post

                  I guess it's because the leading space in code is not really "space" but some other invisible characters, so they cannot be explicitly replaced.
                  Alright, I see. Thanks again! I am curious about what the exact the characters are. I copied the characters in the data frame while it couldn't be replaced.

                  Comment


                  • #10
                    Use charlist (or better chartab) from SSC to identify characters. Here is one possible culprit:

                    Code:
                    . di ("|" + uchar(160) + "|")
                    | |

                    Comment


                    • #11
                      Originally posted by Nick Cox View Post
                      Use charlist (or better chartab) from SSC to identify characters. Here is one possible culprit:

                      Code:
                      . di ("|" + uchar(160) + "|")
                      | |
                      How do you identify this?
                      I found there exist horizontal tabulation in my vaariable "code"
                      Click image for larger version

Name:	1.png
Views:	1
Size:	91.4 KB
ID:	1639104

                      Comment


                      • #12
                        how to delete the uchar(9) then?

                        Comment


                        • #13
                          Code:
                           
                           replace `v'=usubinstr(`v', uchar(9), "", .)

                          Comment


                          • #14
                            Originally posted by Nick Cox View Post
                            Code:
                            replace `v'=usubinstr(`v', uchar(9), "", .)
                            Thank you!

                            Comment

                            Working...
                            X