==========================

面向对象编程进阶和字符串

==========================

Δ一.类的设计模式—单例

【单例】程序允许过程中,有且仅有一块内存空间存在,这种类的设计模式成为单例

【问】什么时候用到单例

【答】数据共享的时候

以Apple为例:

+ (Apple *)share

{

static Apple * a = nil;

static dispatch_once_t onceToken;

dispatch_once(&onceToken,^{

a = [[Apple alloc] init];

});

return a;

}

1.单例的书写格式

以+(加号)开头的方法,可以直接被类调用;-(减号)开头方法 ,是对象方法,用对象来调用;

【注】单例方法名字随便起,但是尽量保证见名知意!

====================

字符串

====================

一.认识oc中的字符串

oc中以 @“” 包含的内容,是字符串;无论@“”包含了字符串的长度为多少。

【例如】:@“a”,@“123”,@“_%^&*”,

【注】c语言中字符串是用“”,oc中是用@“”

NSString

【见strDemo1】

//声明了一个类型为NSString 的字符串str

//NSString 是一个类的类型,用法跟基础类型一样

//NSString 这个类是苹果公司封装好的一个字符串类

//定义了一个str 这个对象变量,给赋初始值为@"hello world!"

NSString* str = @"hello world!";

//c字符串

//        char a[64]="hello";

//        a[0],a[1]...

//        NSString* str1 = [[NSString alloc]initWithFormat:@"%@",str];

//        NSLog(@"%@",str1);

//求字符串长度 --- length

//oc中 常用int类型,NSUInteger不常用

/**************************************************************************************/

1>

NSUInteger length = [str length];

NSLog(@"%ld",length);

2>

//∆万能拼接字符串

NSString* Str1 = @"yudejun";

NSString* Str2 = @"xijinping";

3>

//oc 中 字符串的格式转化符是:%@

//编译器在碰到格式化转化符的时候,会自动替换成后面参数所对应的变量值

NSString* Str3 = [NSString stringWithFormat:@"%@ and %@",Str1,Str2];

NSLog(@"%@",Str3);

4>

  例如:

//请拼接2个字符串,第一个字符串内容为@“你好”,第二个字符串为@“中国”;拼接完成之后打印出长度

NSString* str4 = @"你好";

NSString* str5 = @"中国";

NSString* str6 = [NSString stringWithFormat:@"%@%@",str4,str5];

NSLog(@"%ld",[str6 length]);

5>

//字符串的转化

NSString* StrInt = @"123456789987654321";

int a = [StrInt intValue];

NSLog(@"%d",a);

long long b = [StrInt longLongValue];

//@“0.1234”

//NSString* StrFloat = @"0.1234";

float c = [@"0.1234" floatValue];

//练习 : 计算@“1234” @“0.25” @“199” @“0.5” 这四个数的值,并打印出来

6>

//字符串分割

//【注】字符串分割要在字符串有效长度范围内

NSString* tempStr = @"helloworld";

//        NSString* FStr = [tempStr substringFromIndex:5];

//        NSString* FStr = [tempStr substringToIndex:5];

NSRange range = {2,3};

NSString* FStr = [tempStr substringWithRange:range];

NSLog(@"%@",FStr);

7>

//字符串的比较

NSString* tem1 = @"abc";

NSString* tem2 = @"abc";

//判断tem1 是否等于 tem2

if ([tem1 isEqualToString:tem2]) {

NSLog(@"相等");

}

else

{

NSLog(@"不相等");

}

NSComparisonResult result = [tem1 compare:tem2];

if (result == NSOrderedAscending) {

NSLog(@"tem2 > tem1");

}

else if (result == NSOrderedDescending)

{

NSLog(@"tem2 < tem1");

}else if (result == NSOrderedSame)

{

NSLog(@"tm2 == tem1");

}

8>

//BOOL 读作布尔类型,只有2种状态,真或者假,可以用1、0也可以用yes 、no

//字符串前缀和后缀判断

NSString* string = @"http://www.baidu.com";

BOOL have = [string hasPrefix:@"http://"];

if (have == YES) {

NSLog(@"是http://开头");

}

else

{

NSLog(@"不是http://开头");

}

BOOL havejiewei = [string hasSuffix:@"com"];

if (havejiewei) {

NSLog(@"是com结尾");

}

else

{

NSLog(@"不是com结尾");

}

9>

//转成大写

NSLog(@"%@",[string uppercaseString]);

//转成小写

[string lowercaseString];

//所有单词首字母大写

[string capitalizedString];

//查找字符串中的某个字符串,进行替换,第一个参数是要查找的字符串,第二个字符串为需要替换成的字符串

string = [string stringByReplacingOccurrencesOfString:@"www" withString:@"googl"];

NSLog(@"%@",string);

NSString* tmp = @"ceshishuju";

//=========================================

NSMutableString* m_str = [[NSMutableString alloc]initWithString:tmp];

//字符串的修改

[m_str setString:@"hello world"];

//字符串的追加

[m_str appendFormat:@" nihao"];

//字符串的删除

NSRange rang = {1,2};

[m_str deleteCharactersInRange:rang];

//字符串的插入

[m_str insertString:@"xijinping" atIndex:3];

NSLog(@"%@",m_str);

//∆以NSMutable开头的都是可变类型,以NSMutable开头定义的对象都是可以对数据进行修改;

//        NSString //不可变字符串

//        NSMutableString//可变字符串

//        NSArray// 不可变数组

//        NSMutableArray//可变数组

//        NSDictionary//不可变字典

//        NSMutableDictionary//可变字典

//可变的意思是可以对这个类型定义的变量进行修改或者删除操作

OC-NSString的更多相关文章

  1. OC NSString(字符串)

    OC NSString(字符串) 多行文字字面量 NSString * string = @"abC" @"DEF" @"hjk" @&qu ...

  2. iOS学习13之OC NSString类

    C语言中,字符串是有char(ASC||码)字符组成. OC中,字符串是由unichar(Unicode)字符组成. 1.字符串(NSString) NSString:不可变字符串,即:创建以后,内容 ...

  3. iOS - OC NSString 字符串

    前言 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> @interface NSM ...

  4. OC NSString 基本操作(用到补充持续更新)

    1.将字符串拆分成数组 NSString *string = @"1,2,3,4"; NSArray *array = [string componentsSeparatedByS ...

  5. OC——NSString的常用方法

    NSString *str1 = @"BeiJing"; NSString *str2 = @"beijing"; //全部转为大写 NSLog(@" ...

  6. OC——NSString和NSMutableString

    int main(int argc, const char * argv[]) { @autoreleasepool { //----------------NSString------------- ...

  7. OC Nsstring的使用

    // // main.m // NSString // // Created by mj on 13-4-5. // Copyright (c) 2013年 itcast. All rights re ...

  8. C 和 OC 字符串转换 NSString 和 char * 转换 const char* 与 char *

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { char *s = "He ...

  9. OC中的字符串常用方法

    OC中的字符串常用方法 OC中对字符串进行操作使用了Foundation框架中的NSString类(不可变).NSMutableString类(可变). NSString 1.创建字符串 [objc] ...

  10. 记录OC学习的一点一滴(二)

    NSString 基础练习: 代码: // // main.m // NSStringDemo01 // // Created by Levi on 14-3-14. // Copyright (c) ...

随机推荐

  1. Ubuntu16.04配置Android SDK环境

    下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html(注意32位与64位,我 ...

  2. Thinkphp5.0实战开发二------自动生成目录结构

    序言 ThinkPHP5.0 具备自动创建功能,可以用来自动生成需要的模块及目录结构和文件等,自动生成主要调用\think\Build 类库.ThinkPHP5.0中模块文件夹在application ...

  3. Jenkins 安装教程

    第一部分,安装Jenkins 1.首先在Jenkins repo yum源和Key [root@jenkins ~]# wget http://pkg.jenkins.io/redhat-stable ...

  4. IPTABLES拒绝某个IP某项服务,并记录到日志(rhel7实例)

    #iptables -I INPUT -p icmp -s 192.168.0.1 -j DROP                 \\在INPUT链中插入:如果检测到从192.168.0.1发过来的 ...

  5. Elasticsearch Server,2nd Edition pdf 翻译 中文

    很偶然的机会,就需要接触到搜索,入门就是google trend已然超过solr的ES.在入门的时候找书的时候发现没有中文版的.于是自己开始翻译Elasticsearch Server,2nd Edi ...

  6. ng2 quickstart

    1.下载 git clone https://github.com/angular/quickstart.git quickstart-angular 2.安装模块 npm install 3.启动 ...

  7. JAVA 单向链表

    package com.session.link; /** * 单向链表 */public class LinkedList<T> { private Node head;//指向链表头节 ...

  8. matplotlib 初步学习

    author:pprp Matplotlib数据可视化 [TOC] 安装 conda install matplotlib sudo apt-get install python-matplotlib ...

  9. helloworld:一个完整的WCF案例

    服务端 1.创建一个空的解决方案:WCFDemo: 2.创建一个宿主控制台程序:Host 3.右击Host项目,选择“添加”--“新建项”,选择“WCF服务”创建名为“Service1.cs”的服务 ...

  10. 机器学习笔记—Logistic 回归

    前面我们介绍了线性回归,为捕获训练集中隐藏的线性模型,提高预测准确率,我们寻找最佳参数 θ,使得预测值与真实值误差尽量小,也就是使均方误差最小.而经过验证,最小均方误差是符合最大似然估计理论的. 在 ...