OC-获取城市首字母

题目:解析根据文件里面的内容,读入一个城市,输出所在首字母
比如读入 长春 输出 c
读入 北京 输出 b

从如下文件解析

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 3 <plist version="1.0">
 4 <dict>
 5     <key>a</key>
 6     <array>
 7         <string>安吉</string>
 8         <string>安庆</string>
 9         <string>鞍山</string>
10         <string>澳门</string>
11     </array>
12     <key>b</key>
13     <array>
14         <string>保定</string>
15         <string>宝鸡</string>
16         <string>包头</string>
17         <string>北海</string>
18         <string>北京</string>
19         <string>蚌埠</string>
20         <string>滨州</string>
21     </array>
22     <key>c</key>
23     <array>
24         <string>沧州</string>
25         <string>长春</string>
26         <string>常德</string>
27         <string>长乐</string>
28         <string>长沙</string>
29         <string>常熟</string>
30         <string>常州</string>
31         <string>潮阳</string>
32         <string>潮州</string>
33         <string>承德</string>
34         <string>成都</string>
35         <string>郴州</string>
36         <string>重庆</string>
37         <string>滁州</string>
38         <string>慈禧</string>
39         <string>从化</string>
40     </array>
41 </dict>
42 </plist>

本次思路

1.创建一个首字母和城市名称的一个类FirstLetterAndCity,其中有两个成员变量,一个位字符串类型,存储首字母,一个动态数组,存储该字母对应的所有城市。

 1 //
 2 //  FirstLetterAndCity.h
 3 //  day16_OC5_城市首字母
 4 //
 5 //  Created by qianfeng on 15/6/15.
 6 //  Copyright (c) 2015年 QF_Lpj. All rights reserved.
 7 //
 8
 9 #import <Foundation/Foundation.h>
10
11 @interface FirstLetterAndCity : NSObject
12 {
13     NSString *_firstLetter;  //城市首字母
14     NSMutableArray *_cityList; //城市集合
15 }
16 - (id)init;
17
18 //设置城市首字母
19 - (void)setFirstLetter:(NSString *)firstLetter;
20 - (NSString *)firstLetter;
21
22 //首字母和城市类的城市列表中添加城市
23 - (void)addCityList:(NSString *)cityName;
24 - (NSMutableArray *)cityList;
25
26
27 @end
28 ------------------------------
29 //
30 //  FirstLetterAndCity.m
31 //  day16_OC5_城市首字母
32 //
33 //  Created by qianfeng on 15/6/15.
34 //  Copyright (c) 2015年 QF_Lpj. All rights reserved.
35 //
36
37 #import "FirstLetterAndCity.h"
38
39 @implementation FirstLetterAndCity
40
41 - (id)init
42 {
43     if (self = [super init]) {
44         _firstLetter = @"";
45         _cityList = [[NSMutableArray alloc] init];
46     }
47     return self;
48 }
49
50 //设置城市首字母
51 - (void)setFirstLetter:(NSString *)firstLetter
52 {
53     _firstLetter = firstLetter;
54 }
55 - (NSString *)firstLetter
56 {
57     return _firstLetter;
58 }
59
60 //首字母和城市类的城市列表中添加城市
61 - (void)addCityList:(NSString *)cityName
62 {
63     if (![cityName isEqualToString:@""]) {
64         [self ->_cityList addObject:cityName];
65     }
66 }
67 - (NSMutableArray *)cityList
68 {
69     return _cityList;
70 }
71
72
73 @end

2.创建一个城市管理类,其中有一个动态数组的成员变量,用于存储所有FirstLetterAndCity类对象。

之后通过遍历管理类中的数组,对每一个FirstLetterAndCity对象中的数组再意义遍历,当对应的城市名称在其中有时,就返回该对象对应的城市首字母

  1 //
  2 //  ManagerCity.h
  3 //  day16_OC5_城市首字母
  4 //
  5 //  Created by qianfeng on 15/6/15.
  6 //  Copyright (c) 2015年 QF_Lpj. All rights reserved.
  7 //
  8
  9 #import <Foundation/Foundation.h>
 10 #import "FirstLetterAndCity.h"
 11
 12 #define FilePath @"/Users/qianfeng/Desktop/day16_OC5_复合类设计/day16_OC5_城市首字母/城市首字母.txt"
 13
 14 @interface ManagerCity : NSObject
 15 {
 16     NSMutableArray *_firstLetterAndCityList;  //用于存储城市集合和首字母  FirstLetterAndCity 对象
 17 }
 18
 19 - (id)init;
 20
 21 - (NSString *)getFileAllString:(NSString *)filePath;
 22
 23 //输入一个城市名称,返回对应的首字母
 24 - (NSString *)getFirstLetterByCityName:(NSString *)cityName;
 25
 26
 27 + (void)testManagerCity:(NSString *)cityName;
 28
 29
 30
 31 @end
 32
 33 ---------------------------------------------------
 34 //
 35 //  ManagerCity.m
 36 //  day16_OC5_城市首字母
 37 //
 38 //  Created by qianfeng on 15/6/15.
 39 //  Copyright (c) 2015年 QF_Lpj. All rights reserved.
 40 //
 41
 42 #import "ManagerCity.h"
 43 #import "FirstLetterAndCity.h"
 44
 45 //声明一个匿名类别 实现私有化
 46 @interface ManagerCity ()
 47
 48 - (NSString *)getFLetterByCityName:(NSString *)cityName;
 49
 50 @end
 51
 52 @implementation ManagerCity
 53
 54 - (id)init
 55 {
 56     if (self = [super init]) {
 57         _firstLetterAndCityList = [[NSMutableArray alloc] init];
 58     }
 59     return self;
 60 }
 61
 62 //由路径生成一个字符串对象
 63 - (NSString *)getFileAllString:(NSString *)filePath
 64 {
 65     NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
 66     return fileContent;
 67 }
 68
 69 //通过城市名称获取城市首字母
 70 - (NSString *)getFirstLetterByCityName:(NSString *)cityName
 71 {
 72     NSString *fileContent = [self getFileAllString:FilePath];
 73     NSArray *fileContentArray = [fileContent componentsSeparatedByString:@"\n"];
 74     //首字母 城市名类
 75     FirstLetterAndCity *letterCity;
 76     if (fileContentArray) {
 77         for (NSString *item in fileContentArray) {
 78             if ([item isEqualToString:@""]) {
 79                 continue;
 80             }
 81             else if([item containsString:@"<key>"])
 82             {
 83                 letterCity = [[FirstLetterAndCity alloc] init];
 84                 //<key>x</key> 获取‘x‘前后的位置坐标,由此获取到x
 85                 NSRange rangeRight = [item rangeOfString:@">"];
 86                 NSRange rangeLeft = [item rangeOfString:@"</"];
 87                 [letterCity setFirstLetter:[item substringWithRange:NSMakeRange(rangeRight.location+1, rangeLeft.location-rangeRight.location-1)]];
 88
 89             }else if([item containsString:@"<string>"])
 90             {
 91                 //获取"<string>XX</string>"  XX之间的位置,并且由此获得对应的城市
 92                 NSRange rangeRight = [item rangeOfString:@">"];
 93                 NSRange rangeLeft = [item rangeOfString:@"</"];
 94                 [letterCity addCityList:[item substringWithRange:NSMakeRange(rangeRight.location+1, rangeLeft.location-rangeRight.location-1)]];
 95             }
 96             //只有当碰到"</array>"时才说明走完了一个key 和 一列 string
 97             if (letterCity && [item containsString:@"</array>"]) {
 98                 [self->_firstLetterAndCityList addObject:letterCity];
 99             }
100         }
101     }
102
103     NSString *firstLetter = [self getFLetterByCityName:cityName];
104
105
106     return firstLetter;
107 }
108
109 //当获取 _firstLetterAndCityList 列表时候,通过传入的城市名字获取到对应的城市首字母
110 - (NSString *)getFLetterByCityName:(NSString *)cityName
111 {
112     //FirstLetterAndCity *flCity = [[FirstLetterAndCity alloc] init];
113     for (FirstLetterAndCity *flCity in self->_firstLetterAndCityList) {
114         for (NSString *item in [flCity cityList]) {
115             if ([item isEqualToString:cityName]) {
116                 return [flCity firstLetter];
117             }
118         }
119     }
120     return @"NotFound";
121 }
122
123
124 + (void)testManagerCity:(NSString *)cityName
125 {
126     ManagerCity *mCity = [[ManagerCity alloc] init];
127     NSString *firstLetter = [mCity getFirstLetterByCityName:cityName];
128     if (![firstLetter isEqualToString:@"NotFound"]) {
129         NSLog(@"%@",firstLetter);
130     }else{
131         NSLog(@"没有%@城市的首字母",cityName);
132     }
133 }
134 @end
时间: 2024-09-27 08:19:26

OC-获取城市首字母的相关文章

php获取汉字首字母

php获取汉字首字母,可以用于按字母对数据进行检索排序等. 分享下网上找的代码.亲测有效. function getFirstCharter($str){ if(empty($str)){return '';} $fchar=ord($str{0}); if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0}); $s1=iconv('UTF-8','gb2312',$str); $s2=iconv('gb

获取汉字首字母

//获取汉字拼音的第一个字母 static public string GetChineseSpell(string strText) { int len = strText.Length; string myStr = ""; for (int i = 0; i < len; i++) { myStr += getSpell(strText.Substring(i, 1)); } return myStr; } static public string[] GetChinese

C#获取字符首字母

///<summary> /// 获取字符首字母 /// </summary> public static string GetPyChar(string c) { if (string.IsNullOrEmpty(c)) { return ""; } byte[] array = new byte[2]; array = Encoding.Default.GetBytes(c); int i = (short)(array[0] - '\0') * 256 +

ASP.NET获取汉字首字母

/// <summary> /// 获取汉字首字母(可包含多个汉字) /// </summary> /// <param name="strText"></param> /// <returns></returns> public string GetChineseSpell(string strText) { int len = strText.Length; string myStr = ""

奔五的人学iOS:用swift实现获取拼音首字母,支持取一句话中每字拼音首字母

在最近一项目中,遇到获取拼音首字母的问题,查找了一下网上的方法,没有找到合适好用的,于是自己研究了一下,写了以下方法,欢迎交流,希望对各位有帮助. // // PYFirst.swift // 获取拼音首字母,支持取一句话中每字拼音首字母 // Created by 周蜜([email protected]) on 2015/6/1(儿童节). // Copyright (c) 2015年 www.miw.cn. All rights reserved. // import Foundation

//获取拼音首字母

//获取拼音首字母- (NSString *)firstCharactor:(NSString *)aString{    //转成了可变字符串    NSMutableString *str = [NSMutableString stringWithString:aString];    //先转换为带声调的拼音    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);   

获取字符串首字母

#import <Foundation/Foundation.h> /** *  分类 , Category ,类目: 作用:为没有源码的类扩充功能.仅仅能扩充方法不能够扩充实例变量. */ @interface NSString (SayHi) - (void)sayHi; //可以获取首字母,字符串 - (NSString *)firstCharactor; @end #import "NSString+SayHi.h" @implementation NSString

php获取汉字首字母实例

在我们实际开发工作中,有时候需要获取输入汉字的首字母,然后存到库中,方便用户搜索相关信息,下面给出php代码,留做备用: //新添加获取汉子首字,首字字母 function pinyin($zh){ if(empty($zh)){ return ''; } $s1 = iconv("UTF-8","gb2312", $zh); $s2 = iconv("gb2312","UTF-8", $s1);//将字符串的编码从GB231

sql 获取字符串首字母,循环

//字符串首字母 CREATE FUNCTION GetInitialLetter(@ChineseString NVARCHAR(4000)) RETURNS NVARCHAR(4000) AS BEGIN DECLARE @SingleCharacter NCHAR(1), @ReturnString NVARCHAR(4000) SET @ReturnString = '' WHILE LEN(@ChineseString)>0 BEGIN --依次取单个字符 SET @SingleCha