去掉字符串中所有的空格

代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    //去掉字符串是前面,中间,后面所有的空格
    NSString *username=@"  123  4  8  ";
    NSString *newname= [username stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSLog(@"--username--%@--",newname);

}

输出:

2014-11-17 15:56:06.660 iOS中去掉NSString中的空格[31372:60b] --username--12348--

时间: 2024-08-24 23:05:48

去掉字符串中所有的空格的相关文章

js去掉字符串中的所有空格

1.使用js去掉字符串中的所有空格 1.1.定义一个去空格函数方法 function Trim(str,is_global){ var result; result = str.replace(/(^\s+)|(\s+$)/g,""); if(is_global.toLowerCase()=="g") { result = result.replace(/\s/g,""); } return result; } 1.2. 使用此方法去空格,如下

去掉字符串中的空格

1)Trim方法 string   tt=" aaa "; tt=tt.Trim()       去字符串首尾空格的函数 tt=tt.TrimEnd() 去掉字符串尾空格 tt=tt.TrimStart() 去掉字符串首空格 (2)通过ASCII码值去掉字符中的空格 由于空格的ASCII码值是32,因此,在去掉字符串中所有的空格时,只需循环访问字符串中的所有字符,并判断它们的ASCII码值是不是32即可.去掉字符串中所有空格的关键代码如下: CharEnumerator CEnumer

C++去掉字符串中首尾空格和所有空格

c++去掉首尾空格是参考一篇文章的,但是忘记文章出处了,就略过吧. 去掉首尾空格的代码如下: 1 void trim(string &s) 2 { 3 4 if( !s.empty() ) 5 { 6 s.erase(0,s.find_first_not_of(" ")); 7 s.erase(s.find_last_not_of(" ") + 1); 8 } 9 10 } 去掉首尾空格 去掉字符串中所有空格的代码如下: 1 void trim(string

将字符串中多余的空格去掉

#include<stdio.h> #include<stdlib.h> int main(void) { char str[100]; char des[100]; printf("input a string:"); gets(str); int i = 0; int j = 0; int blank_count = 0; char c = str[0]; for(i=1; c!='\0'; i++) { if(c != ' ') { if(blank_co

如何清除字符串中的所有空格

如何清除字符串中的所有空格:关于清除字符串两端的空格已经有介绍了,具体可以参阅javascript如何去掉字符串两端的空格一章节,本章将介绍一下如何去除字符串中的所有空格,代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.softwhy.com/&qu

C# 使用正则表达式去掉字符串中的数字,或者去掉字符串中的非数字

/// 去掉字符串中的数字 public static string RemoveNumber(string key)          {              return Regex.Replace(key, @"\d", "");          } //去掉字符串中的非数字public static string RemoveNotNumber(string key)  {      return Regex.Replace(key, @"

删除字符串中多余的空格

作者 : 卿笃军 今天遇到的一道笔试题,后来百度 了一下,原来是一道新浪的面试题. 题目大意是这样:给你一个任意字符串,要求你删除字符串中多余的空格. 示例:('_'表示空格) 1)"12__abc_98_"     ==> "12_abc_98" 2)"______65_21__54__3_89___"  ==>  "65_21_54_3_89" 思路:设2个下标i,pos一个用于遍历字符串(i),另外一个用于指

请实现一个函数,把字符串中的每个空格替换成“%20”

请实现一个函数,把字符串中的每个空格替换成"%20".例如输入"we are happy.",则输出"we%20are%20happy." #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { char arr[] = "we are happy"; int i = 0; int j = 0; int len

17、如何对字符串进行左, 右, 居中对齐 18、如何去掉字符串中不需要的字符 19、如何读写文本文件 20、如何处理二进制文件 21、如何设置文件的缓冲

17.如何对字符串进行左, 右, 居中对齐 info = "GBK" print(info.ljust(20)) print(info.ljust(20,'#')) print(info.rjust(20,'#')) print(info.center(20,"#")) print(format(info,'<20')) print(format(info,'>20')) print(format(info,'^20')) result: GBK GBK