截取字段到倒数第2个字

*检查城市名称是否都以一个字结尾。
  gen city2 = substr(city,-3,.)
  tab city2  //所有城市都是以一个字结尾,那么我只用去掉最后一个字就行。

If n1 < 0, n1 is interpreted as the distance from the end of the string; if n2 = . (missing), the remaining portion of the string is returned.

substr("abcdef",2,3)  = "bcd"
substr("abcdef",-3,2) = "de"
substr("abcdef",2,.)  = "bcdef"
substr("abcdef",-3,.) = "def"
substr("abcdef",2,0)  = ""
substr("abcdef",15,2) = ""
  gen city_1 = substr(city,1,strlen(city)-3) //截取到倒数第2个字。
 strlen(s)
       Description:  the number of characters in ASCII s or length in bytes

                     strlen() is intended for use with only plain ASCII characters and for use by programmers who want to obtain the
                     byte-length of a string.  Note that any Unicode character beyond ASCII range (code point greater than 127) takes more
                     than 1 byte in the UTF-8 encoding; for example, é takes 2 bytes.

                     For the number of characters in a Unicode string, see ustrlen().

                     strlen("ab") = 2
                     strlen("é") = 2
       Domain s:     strings
       Range:        integers > 0
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容