欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

oc中的字符串学习

程序员文章站 2024-01-14 22:24:28
...
//
//  main.m
//  text7
//
//  Created by 骆超 on 2020/3/3.
//  Copyright © 2020 luochao.com. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
//        NSString *greeting = @"hello";
//        NSLog(@"sayHello = %@\n",greeting);
        
        NSString *language = @"Objective-c";
        NSString *sayhello = [[NSString alloc] initWithFormat:@"hello, %@!",language];
        NSLog(@"sayhello = %@\n",sayhello);
        
        int starCount = 10;
        NSString *tip = [[NSString alloc] initWithFormat:@"You got %d stars",starCount];
        NSLog(@"tip = %@\n",tip);
        
        //lowercaseString,转换成小写形式
        NSLog(@"tip.lowercaseString = %@\n",[tip lowercaseString]);
        
        //capitalizedString,转换chen成首字母大写形式
        NSLog(@"tip.capitalizedString = %@\n",[tip capitalizedString]);
        
        //检测字符串中是否含有指定字符串的方法,有则输出1,无则输出0,区分大小写
        NSLog(@"tip.hasprefix = %d\n",[tip hasPrefix:@"YOU"]);
        NSLog(@"tip.hassuffix = %d\n",[tip hasSuffix:@"You"]);
        NSLog(@"containsString = %d\n",[tip containsString:@"Stars"]);
        
        //获得字符串的长度,空格也算
        NSLog(@"tip.length = %lu\n",(unsigned long)[tip length]);
        
        //将字符串转换为数字,实际就是将输出字符串80的int属性的的值,可能是让字符串参与计算吧
        NSString *score = @"80";
        NSLog(@"score = %d\n",[score intValue]);
        
        //同理可得,字符串转浮点类型
        NSString *locationX = @"10.2";
        NSLog(@"locationX = %f\n",[locationX floatValue]);
        
        //判断两个字符串是否相等,相等输出1,不等输出0
        NSLog(@"tip is equal to you got 10 stars = %d\n",[tip isEqualToString:@"You got 10 stars"]);
        
        //创建范围类型的对象range,该范围对象是字符串变量“stars”的范围,包含位置和长度两个值
        NSRange range = [tip rangeOfString:@"stars"];
        NSLog(@"range.location = %lu\n",(unsigned long)range.location);
        NSLog(@"range.length = %lu\n",(unsigned long)range.length);
        
        //获得并输出字符串里的第三个字符
        NSLog(@"tip.characterAtIndex.2 = %c\n",[tip characterAtIndex:2]);
        
        //获得并输出字符串里第九个字符以及后面所以字符
        NSLog(@"tip.substringFromIndex.8 = %@\n",[tip substringFromIndex:8]);
        
        //获得并输出从字符串头部开始,到第九个字符之间的所有字符,这两个输出如果遇到空格是不予输出的,如果输出前9个字符而第九个字符是空格的话,output不会输出空格
        NSLog(@"tip.substringToIndex.8 = %@\n",[tip substringToIndex:8]);
        
        //自定义范围对象,输出第五个位置开始的长度为3的字符串
        NSRange tiprange = NSMakeRange(4, 3);
        NSLog(@"tip.substringWithRange = %\n",[tip substringWithRange:tiprange]);
        
        //将字符串中指定的内容替换为其他内容
        NSString *replaceString = [tip stringByReplacingOccurrencesOfString:@"stars" withString:@"apples"];
        NSLog(@"replacedString = %@\n",replaceString);
        
        //在字符串的末尾添加新的字符串
        NSString *greetingSwift = [tip stringByAppendingString:@" Swift!"];
        NSLog(@"greetingSwift = %@\n",greetingSwift);
        
        //一种消除字符串中特殊符号的方法,使用特殊字符串集合对指定字符串进行清除
        NSString *string = @"------Interactive [email protected]#$%^&*()++-";
        NSLog(@"string = %@\n",string);
        //定义特殊字符串集合,hh关于特殊字符集合详见博客转载
        NSCharacterSet *characterset = [NSCharacterSet punctuationCharacterSet];
        string = [string stringByTrimmingCharactersInSet:characterset];
        NSLog(@"Final String = %@",string);
        
        //当旧的字符串的值被重新赋值后,旧的值仍存在于储存空间中,oc不允许字符串可变
//        greetingSwift += @"nice to meet you";
//        NSLog(@"%@\n",greetingSwift);
        
        //使用可变字符串类型,并定义它占用的内存大小,单位字节
        NSMutableString *mutableString = [NSMutableString stringWithCapacity:40];
        NSMutableString *mutableString2 = [[NSMutableString alloc] initWithString:@"This is xxx"];
        //发送消息来添加字符串至尾部
        [mutableString2 appendString:@" MSMutableString."];
        //发送消息来往指定位置插入字符串
        [mutableString2 insertString:@"ooo" atIndex:8];
        NSLog(@"mutableString2 = %@\n",mutableString2);
        
        //发送消息来删除指定范围的字符串,(0,10)从 0 开始,长度是 10
        [mutableString2 deleteCharactersInRange:NSMakeRange(0, 10)];
        NSLog(@"mutableString2.deleteCharactersInRange = %@\n",mutableString2);
        
        //发送消息替换字符串内容
        [mutableString2 setString:@"Clear"];
        NSLog(@"mutableString2 = %@\n",mutableString2);
        
    }
    
    return 0;
}

关于特殊字符数组NSCharacterSet可以看https://www.cnblogs.com/yaoyao0110/p/5413176.html

 

相关标签: object-c