怎么把oc中nsstring字符串变成int型
1个回答
2015-09-24
展开全部
1,字符串的简单用法:
02.#import <Foundation/Foundation.h>
03.
04.int main (int argc, const charchar * argv[]) {
05. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06.
07. //创建常量字符串
08. NSString* str=@"This is a string";
09.
10. //1,获取字符串的长度</span>
11. NSUInteger len=[str length];
12. NSLog(@"length=%lu", len); //result:16
13.
14. //2,获取下标为1的字符,用unichar接收</span>
15. unichar c=[str characterAtIndex:1];
16. NSLog(@"%c", c); //result:h
17.
18. NSLog(@"%lu", sizeof(unichar)); //result:2
19.
20. //3,initWithString初始化字符串</span>
21. str=[[NSString alloc]
22. initWithString@"Hello World"];
23. NSLog(@"%@", str); //result:hello world
24. [str release];
25.
26. //4,用标准c字符串创建oc字符串 :initWithCString 方法
27. str=[[NSString alloc]
28. initWithUTF8String:"This is c string"];
29. NSLog(@"%@", str); //result:This is c string
30. printf("%s\n", [str UTF8String]);
31.
32. [str release];
33.
34. str=[NSString stringWithString:@"Hello iOS"];
35. NSLog(@"%@", str); //result Hello iOS
36.
37. int age=30;
38. //5,创建格式化字符串。
39. str=[[NSString alloc]
40. initWithFormat:@"age=%d", age];
41. NSLog(@"%@", str);
42. [str release];
43. //6,stringWithFormat也可用于拼接字符串</span>
44. str=[NSString stringWithFormat:@"1+1=%d", 1+1];
45.
46. NSLog(@"%@", str);
47.
48.
49. [pool drain];
50. return 0;
51.}
52.2,从文件中读取字符串
53.#import <Foundation/Foundation.h>
54.
55.#define PATH "/Users/kj/Desktop/dict.txt"
56.
57.int main (int argc, const charchar * argv[]) {
58. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
59.
60. NSError* error= www.hbbz08.com nil;
61. //下面也可用 NSString* path=[NSString stringWithUTF8String:PATH];就不用手动释放path了
62. NSString* path=[[NSString alloc] initWithUTF8String:PATH];
63.
64. /*
65. 下面也可用
66. NSString* fileContentString=
67. [[NSString alloc]
68. initWithContentsOfFile:path
69. encoding:NSUTF8StringEncoding
70. error:&error];
71.
72. 但是不要忘了
73. [fileContentString release];
74. */
75. NSString* fileContentString=[NSString
76. stringWithContentsOfFile:path
77. encoding:NSUTF8StringEncoding
78. error:&error]; //如果创建失败,error改变指针指向,指向内部创建的一个NSerror对象
79.
80. [path release];
81.
82. if (nil!=error) {
83. NSLog(@"%@", error);
84. exit(-1);
85. }
86. NSLog(@"%@", fileContentString);
87.
88. [pool drain];
89. return 0;
90.}
91.
92.
93.3,写入文件到字符串
94.
95.NSString *astring = [[NSString alloc]
96. initWithString:@"This is a String!"];
97.
98. NSLog(@"astring:%@",astring);
99. NSString *path = @"astring.text";
100. [astring writeToFile: path atomically: YES];
101. [astring release];
102.
103.4,重写类描述方法
104.
105.
106.
107.#import <Foundation/Foundation.h>
108.
109.@interface Person : NSObject
110.@end
111.
112.@implementation Person
113.#if 1
114.-(NSString*)description
115.{
116. return @"This is Person's object";
117.}
118.#endif
119.@end
120.
121.
122.
123.int main (int argc, const charchar * argv[]) {
124. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
125.
126.
127. Person* p=[[Person alloc] init];
128. NSLog(@"%@", p);
129. [p release];
130.
131. [pool drain];
132. return 0;
133.}
134.
135.重写了类描述方法,我们用NSLog(@"%@", p );就会调用我们自己的方法。
136.调试的时候覆写类描述方法比较有用
137.5,字符串的比较
138.
139.#import <Foundation/Foundation.h>
140.
141.int main (int argc, const charchar * argv[]) {
142. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
143.
144. NSString* str1=@"Hello World";
145. NSString* str2=@"hello World";
146.
147. if([str1 isEqualToString:str2])
148. {
149. NSLog(@"YES");
150. }
151. else
152. {
153. NSLog(@"NO");
154. }
155. //compare方法对应的返回值为enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};</span>
156. NSComparisonResult ret=[str1 compare:str2];
157. if (NSOrderedAscending==ret) {
158. NSLog(@"%@<%@", str1, str2);
159. }
160.
161. if (NSOrderedSame==ret) {
162. NSLog(@"%@=%@", str1, str2);
163. }
164.
165. if (NSOrderedDescending==ret) {
166. NSLog(@"%@>%@", str1, str2);
167. }
168.
169.
170. [pool drain];
171. return 0;
172.}
173.
174.6,字符串的截取
175.
176.
177.#import <Foundation/Foundation.h>
02.#import <Foundation/Foundation.h>
03.
04.int main (int argc, const charchar * argv[]) {
05. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06.
07. //创建常量字符串
08. NSString* str=@"This is a string";
09.
10. //1,获取字符串的长度</span>
11. NSUInteger len=[str length];
12. NSLog(@"length=%lu", len); //result:16
13.
14. //2,获取下标为1的字符,用unichar接收</span>
15. unichar c=[str characterAtIndex:1];
16. NSLog(@"%c", c); //result:h
17.
18. NSLog(@"%lu", sizeof(unichar)); //result:2
19.
20. //3,initWithString初始化字符串</span>
21. str=[[NSString alloc]
22. initWithString@"Hello World"];
23. NSLog(@"%@", str); //result:hello world
24. [str release];
25.
26. //4,用标准c字符串创建oc字符串 :initWithCString 方法
27. str=[[NSString alloc]
28. initWithUTF8String:"This is c string"];
29. NSLog(@"%@", str); //result:This is c string
30. printf("%s\n", [str UTF8String]);
31.
32. [str release];
33.
34. str=[NSString stringWithString:@"Hello iOS"];
35. NSLog(@"%@", str); //result Hello iOS
36.
37. int age=30;
38. //5,创建格式化字符串。
39. str=[[NSString alloc]
40. initWithFormat:@"age=%d", age];
41. NSLog(@"%@", str);
42. [str release];
43. //6,stringWithFormat也可用于拼接字符串</span>
44. str=[NSString stringWithFormat:@"1+1=%d", 1+1];
45.
46. NSLog(@"%@", str);
47.
48.
49. [pool drain];
50. return 0;
51.}
52.2,从文件中读取字符串
53.#import <Foundation/Foundation.h>
54.
55.#define PATH "/Users/kj/Desktop/dict.txt"
56.
57.int main (int argc, const charchar * argv[]) {
58. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
59.
60. NSError* error= www.hbbz08.com nil;
61. //下面也可用 NSString* path=[NSString stringWithUTF8String:PATH];就不用手动释放path了
62. NSString* path=[[NSString alloc] initWithUTF8String:PATH];
63.
64. /*
65. 下面也可用
66. NSString* fileContentString=
67. [[NSString alloc]
68. initWithContentsOfFile:path
69. encoding:NSUTF8StringEncoding
70. error:&error];
71.
72. 但是不要忘了
73. [fileContentString release];
74. */
75. NSString* fileContentString=[NSString
76. stringWithContentsOfFile:path
77. encoding:NSUTF8StringEncoding
78. error:&error]; //如果创建失败,error改变指针指向,指向内部创建的一个NSerror对象
79.
80. [path release];
81.
82. if (nil!=error) {
83. NSLog(@"%@", error);
84. exit(-1);
85. }
86. NSLog(@"%@", fileContentString);
87.
88. [pool drain];
89. return 0;
90.}
91.
92.
93.3,写入文件到字符串
94.
95.NSString *astring = [[NSString alloc]
96. initWithString:@"This is a String!"];
97.
98. NSLog(@"astring:%@",astring);
99. NSString *path = @"astring.text";
100. [astring writeToFile: path atomically: YES];
101. [astring release];
102.
103.4,重写类描述方法
104.
105.
106.
107.#import <Foundation/Foundation.h>
108.
109.@interface Person : NSObject
110.@end
111.
112.@implementation Person
113.#if 1
114.-(NSString*)description
115.{
116. return @"This is Person's object";
117.}
118.#endif
119.@end
120.
121.
122.
123.int main (int argc, const charchar * argv[]) {
124. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
125.
126.
127. Person* p=[[Person alloc] init];
128. NSLog(@"%@", p);
129. [p release];
130.
131. [pool drain];
132. return 0;
133.}
134.
135.重写了类描述方法,我们用NSLog(@"%@", p );就会调用我们自己的方法。
136.调试的时候覆写类描述方法比较有用
137.5,字符串的比较
138.
139.#import <Foundation/Foundation.h>
140.
141.int main (int argc, const charchar * argv[]) {
142. NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
143.
144. NSString* str1=@"Hello World";
145. NSString* str2=@"hello World";
146.
147. if([str1 isEqualToString:str2])
148. {
149. NSLog(@"YES");
150. }
151. else
152. {
153. NSLog(@"NO");
154. }
155. //compare方法对应的返回值为enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};</span>
156. NSComparisonResult ret=[str1 compare:str2];
157. if (NSOrderedAscending==ret) {
158. NSLog(@"%@<%@", str1, str2);
159. }
160.
161. if (NSOrderedSame==ret) {
162. NSLog(@"%@=%@", str1, str2);
163. }
164.
165. if (NSOrderedDescending==ret) {
166. NSLog(@"%@>%@", str1, str2);
167. }
168.
169.
170. [pool drain];
171. return 0;
172.}
173.
174.6,字符串的截取
175.
176.
177.#import <Foundation/Foundation.h>
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询