iOS學習筆記2 NSString

2021-07-09 11:48:28 字數 4146 閱讀 8844

主要分為兩個部分1. nsstring 2. nsmutablestring

一. nsstring

char* s = "hello";

nsstring* str = @"hello";

// oc, c 字串轉換

// c->oc

nsstring* str1 = [nsstring stringwithutf8string:s];

nslog(str1);

// oc->c

nslog(@"str2 = %s", [str utf8string]);

// 建立字串

nsstring* str3 = @"ios"; // 相當於系統自動給你分配記憶體不需要手動釋放

nsstring* str4 =[[nsstring alloc] init]; // 需要手動釋放

str4 = @"ios";

// 格式化字串

int a = 10;

int b = 20;

nsstring* str5 = [nsstring stringwithformat:@"a= %d b = %d", a, b];

nslog(@"str5 = %@", str5);

// 拼接字串

nslog(@"str6 = %@", str6);

// 大小寫轉換

nsstring* str7 = @"abcdef";

nsstring* str8 = [str7 lowercasestring];

nslog(@"str8 = %@", str8);

nsstring* str9 = [str7 uppercasestring];

nslog(@"str9 = %@", str9);

//字首和字尾的判斷

nsstring* str10 = @"www.imooc.com";

//判斷字首

bool hasprefix = [str10 hasprefix:@"www."];

if(hasprefix)

else

//判斷字尾

bool hassuffix = [str10 hassuffix:@".com"];

if(hassuffix)

else

// 判斷字串相同

nsstring* str11 = @"hello";

nsstring* str12 = @"hello";

if([str11 isequaltostring:str12])

else

// 分割字串

// 按照指定字元分隔字元穿

nsstring* str13 = @"a,b,c,d,e";

nsarray* strarray = [str13 componentsseparatedbystring:@","];

for(nsstring* str in strarray)

// 按照範圍擷取字串

nsrange range = nsmakerange(1, 5); // nsrange 不是乙個類,而是乙個struct,所以不是指標

nsstring* str14 = [str13 substringwithrange:range];

nslog(@"str14 = %@", str14);

// 從某一位開始擷取後面的字串

nsstring* str15 = [str13 substringfromindex:2];

nslog(@"str15 = %@", str15);

// 從開頭擷取到某一位

nsstring* str16 = [str13 substringtoindex:5];

nslog(@"str16 = %@", str16);

// 將字串拆分位每乙個字元

for(int i = 0; i < [str13 length]; i++)

// 查詢字串

nsstring* str17 = @"ab cd ef gh ij";

// 查詢指定字串的位置

nsrange range1 = [str17 rangeofstring:@"ab"];

nslog(@"range1.location is %d, rang1.length is %d ", range1.location, range1.length);

// 替換

nsstring* str18 = @"hello ios, hello duke";

//替換某一部分內容

nsstring* str19 = [str18 stringbyreplacingcharactersinrange:nsmakerange(0, 5) withstring:@"你好"];

nslog(@"str19 = %@", str19);

// 用指定字串替換原字串中的字串

nsstring* str20 = [str18 stringbyreplacingoccurrencesofstring:@"hello" withstring:@"你好"];

nslog(@"str20 = %@", str20);

// 讀取檔案

// 檔案**,1. 本地檔案 2. 網路檔案

// 路徑類

// 網路路徑

// 本地路徑

nsurl* fileurl = [nsurl fileurlwithpath:@"/users/lumingming/desktop/test.txt"];

//讀取網路檔案

// 寫入檔案

nsstring* str22 = @"hello fred";

[str22 writetofile:@"/users/lumingming/desktop/demo.txt" atomically:yes encoding:nsutf8stringencoding error:nil];

二. nsmutablestring

// 可變字串是字串的子類

nsmutablestring* str = [[nsmutablestring alloc] initwithcapacity:10];

[str setstring:@"hello"];

//str = @"hello"; //左邊這種寫法不正確,這是nsstring的賦值方法,不適用於nsmutablestring

// 1. 追加字串

nslog(@"str is %@", str);

int a = 10;

// 2. 追加格式化字串

nslog(@"str is %@", str);

// 3. 替換字串

nsrange range = [str rangeofstring:@"world"];

[str replacecharactersinrange:range withstring:@"fred"];

nslog(@"str is %@", str);

// 4. 插入字串

[str insertstring:@"android" atindex:6];

nslog(@"str is %@", str);

// 5. 刪除字串

nsrange range1 = [str rangeofstring:@"fred"];

[str deletecharactersinrange:range1];

nslog(@"str is %@", str);

ios學習筆記2

1 uitableviewcell預設有乙個子控制項contentview。可以如下驗證 nslog cell p content p p p p cell,cell.contentview,cell.imageview.superview,cell.textlabel.superview,cell...

IOS學習筆記

uiview beginanimations view flip context nil 設定動畫塊 uiview setanimationduration 1.25 動畫時間 uiview setanimationcurve uiviewanimationcurveeaseinout 動畫曲線 u...

ios學習筆記

actionsheet 標頭檔案裡加協議。ibaction buttonpressed id sender void actionsheet uiactionsheet actionsheet diddismisswithbuttonindex nsinteger buttonindex 從plis...