1 string str = " asdf asd saddf sdfwrqeqw a asdf "; 2 string[] strs = str.Trim().Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries); 3 string finallStr = string.Join(" ",strs);
项目中用到历史标签,需要去掉字符串两端的空格,但是不能去掉字符串中间的空格,所以用到了下边的方法
NSString *history = [self.windowMarkTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
亲测可用。
如果想去掉字符串的空格只需要查找到空格替换就行了
NSString *history = [self.windowMarkTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
javascript如何去掉字符串两端空格:
在表单登陆验证的时候一般要首先字符串两端的空格,当然并不仅仅局限于表单登陆,很多地方都需要这样操作,不过javascript中并没有和c#等语言自带有trim()函数,需要通过模拟实现才行。
代码如下:<script type="text/javascript"> function trim(str){ //删除左右两端的空格 return str.replace(/(^\s*)|(\s*$)/g, ""); } function ltrim(str){ //删除左边的空格 return str.replace(/(^\s*)/g,""); } function rtrim(str){ //删除右边的空格 return str.replace(/(\s*$)/g,""); } </script>
以上代码可以删除字符串两端的空格。
原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=8173
更多内容可以参阅:http://www.softwhy.com/javascript/
转载于:https://blog.51cto.com/11038108/1727346
1 string str = " asdf asd saddf sdfwrqeqw a asdf "; 2 string[] strs = str.Trim().Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries); 3 string finallStr = string.Join(" ",strs);
转载于:https://www.cnblogs.com/dotnetHui/p/7999924.html
public static void main(String[] args){
fu6();
}public static void fu6(){
String a=" aasdsa ";
System.out.print("原函数"+a+" ");
String a1=a.trim();
System.out.println("修改后"+a1);
}转载于:https://www.cnblogs.com/hfew/p/10561343.html