Contents [hide]
输出结果
aaa
程式码
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <stdio.h> #include <string.h> #include <stdlib.h> char *ltrim( char *str, const char *seps) { size_t totrim; if (seps == NULL) { seps = "\t\n\v\f\r " ; } totrim = strspn (str, seps); if (totrim > 0) { size_t len = strlen (str); if (totrim == len) { str[0] = '\0' ; } else { memmove (str, str + totrim, len + 1 - totrim); } } return str; } char *rtrim( char *str, const char *seps) { int i; if (seps == NULL) { seps = "\t\n\v\f\r " ; } i = strlen (str) - 1; while (i >= 0 && strchr (seps, str[i]) != NULL) { str[i] = '\0' ; i--; } return str; } char *trim( char *str, const char *seps) { return ltrim(rtrim(str, seps), seps); } int main( void ) { char str[] = " aaa \n\t" ; trim(str, NULL); printf ( "%s\n" , str); return 0; } |
C++
1 2 3 4 5 6 7 8 9 10 | #include <iostream> using namespace std; int main() { const std::string& chars = "\t\n\v\f\r " ; std::string str = " aaa \n\t" ; str.erase(str.find_last_not_of(chars) + 1); str.erase(0, str.find_first_not_of(chars)); std::cout << str << endl; } |
C#
1 2 3 4 5 6 7 8 9 10 11 | using System; public class Trim { public void printTrim() { String str = " aaa \n\t"; Console.WriteLine(str.Trim()); } public static void Main() { Trim tm = new Trim(); tm.printTrim(); } } |
Go
1 2 3 4 5 6 7 8 | package main import "fmt" import "strings" func main() { str := " aaa \n\t" fmt.Println(strings.TrimSpace(str)) } |
Java
1 2 3 4 5 6 | public class Trim { public static void main(String[] args) { String str = " aaa \n\t" ; System.out.println(str.trim()); } } |
JavaScript
1 2 | let str = " aaa \n\t" ; console.log(str.trim()); |
Perl
1 2 3 | my $a = " aaa \n\t" ; $a =~ s!^\s*|\s*$!!gs; print ( $a . "\n" ); |
PHP
1 2 3 | <?php $a = " aaa \n\t" ; print (trim( $a ). "\n" ); |
Python
1 2 | str = " aaa \n\t" print ( str .strip()) |
Ruby
1 2 | str = " aaa \n\t" print(str.strip()+ "\n" ) |
Rust
1 2 3 4 | fn main() { let str = " aaa \n\t"; println!("{}", str.trim()); } |
Shell
1 | echo " aaa \n\t" | xargs ; |
![](https://vpshikaku.com/app/uploads/vpshikaku.com/2nZbntr9-e1587540364868.jpg)
作者: 栗田 創。1st Class, inc的首席执行官/首席技术官。搜索引擎工程师、网站管理员
证书: AWS Solution Architect,Professional Scrum Master I ,一级IT工程师(日本国家证书),TOEIC900+,东京大学経済学部経営学科卒业
请关注我的Weblio帐户以获取更新
IT相关服务:VPS排名