zoukankan      html  css  js  c++  java
  • 字符串替换 连接

     NSString *str=[[NSString alloc]initWithFormat:@"123-456-789-000"];
    2         
    3         //(1)把”-“去掉,并放在数组内  并输出
    4         NSArray *arr=[str componentsSeparatedByString:@"-"];
    5         NSLog(@"%@%@%@%@",[arr objectAtIndex:0],[arr objectAtIndex:1],[arr objectAtIndex:2],[arr objectAtIndex:3]);
    6         //(2)用空字符代替“-“
    7         NSString *newStr=[str stringByReplacingOccurrencesOfString:@"-" withString:@""];
    8         NSLog(@"%@",newStr);
    //
    //  main.m
    //  作业2
    //
    //  Created by syrcfwzx on 16/1/6.
    //  Copyright (c) 2016年 syrcfwzx. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            //方法1
            NSString* str = @"123-456-789-000";
            NSString* newstr =[str stringByReplacingOccurrencesOfString:@"-" withString:@""];
            NSLog(@"%@",newstr);
            //方法2
            NSArray* array = [str componentsSeparatedByString:@"-"];
            str = [array componentsJoinedByString:@""];
            NSLog(@"%@",str);
            
        }
        return 0;
    }
    

     例子2

    //
    //  main.m
    //  数组
    //
    //  Created by syrcfwzx on 16/1/7.
    //  Copyright (c) 2016年 syrcfwzx. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            //字符串对象分割
            NSString* str = @"how are you";
            NSArray* words = [str componentsSeparatedByString:@" "];
            NSLog(@"%@",words);
            //字符串对象的拼接
            NSArray* words2 = @[@"one",@"two",@"three"];
            NSString* str2 = [words2 componentsJoinedByString:@"-"];
             NSLog(@"%@",str2);
            
        }
        return 0;
    }
    
  • 相关阅读:
    Redis 安全
    Redis 数据备份与恢复
    Redis 服务器
    Redis 连接
    Redis 脚本
    Linux中使用netstat命令的基本操作,排查端口号的占用情况
    ElasticSearch 常用查询语句
    GO代码风格指南 Uber Go (转载)
    coding 注意事项(总结中)
    Byte字节
  • 原文地址:https://www.cnblogs.com/hezhuangzhuang/p/5115474.html
Copyright © 2011-2022 走看看