zoukankan      html  css  js  c++  java
  • Objective-C字符串

    (1)创建字符串

    NSString* str = @"Hello World";

    NSString* str = [ [NSString alloc] initWithUTF8String:"Hello World"]; //C语言字符串

    NSString* str = [ [NSString alloc] initWithFormat: @"Hello %d", 10];

    还可以利用initWithData:encoding:直接解码二进制数据

    (2)字符串转换成其它数据类型

    NSString* str = @"100";

    int intVar = str.intValue;

    其它数据类型相似的方法。

    (3)字符串连接

    NSString* str = @"Hello";

    NSString* str1 = [ [str stringByAppendingString:@" World"];

    还可以利用stringByAppendingFormat等等。

    (4)字符串的截取

    NSString* str = @"Hello World";

    NSString* str1 = [str substringToIndex:5]; //取前五个字符

    NSString* str1 = [str substringFromIndex:6]; //取后五个字符,注意index是6

    NSRange r = {1,2}; //or NSRangeFromString(@"{1,2}")

    NSString* str1 = [str substringWithRange:r];

    注:在MAC上查看帮助文档的方式是三指点击类型,或者选中类型在help菜单中选择quick help for selected item

  • 相关阅读:
    特殊集合
    推箱子
    集合
    数组

    循环语句 练习题
    穷举与迭代
    循环语句
    练习题
    switch case
  • 原文地址:https://www.cnblogs.com/jacky1982/p/7524161.html
Copyright © 2011-2022 走看看