iOS字串處理

2022-07-15 09:48:12 字數 2180 閱讀 8019

nsstring* string; 

nsstring* string1, string2;

//方法1.

string = [nsstring initwithformat:@"%@,%@", string1, string2 ];

//方法2.(常用)

//方法3 .

清除字串 首尾空格及換行符

//清除首尾空格

//定義巨集

#define alltrim(object) [object stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]]

//呼叫巨集

nsstring *emptystring = @" ";

if ([alltrim( emptystring ) length] == 0 )

//清除所有空格(替換操作)

nsstring *strurl = [urlstring stringbyreplacingoccurrencesofstring:@" " withstring:@""];

//清除換行字元

headerdata = [headerdata stringbyreplacingoccurrencesofstring:@"\r" withstring:@""];

headerdata = [headerdata stringbyreplacingoccurrencesofstring:@"\n" withstring:@""];

//見過最好的解決方法之一, 改進的內聯函式,源自git hub

// check if the "thing" pass'd is empty

static inline bool isempty(id thing)

//判斷uitextfield.text是否為空

使用者未進行任何輸入的情況下為nil. (yourtextfield.text 在最初建立後的值為nil,nil不等於@"")

[yourtextfield.text isequaltostring:@""] || yourtextfield.text == nil

//或者

yourtextfield.text.length == 0

//其他方法一

//判斷字串是否為空

#define strisempty(str) ([str iskindofclass:[nsnull class]] || str == nil || [str length] < 1 ? yes : no )

//方法二

- (bool) isblankstring:(nsstring *)string

if ([string iskindofclass:[nsnull class]])

if ([[string stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]] length]==0)

return no;

}

//定義列舉型別

typedef ns_enum(nsinteger, subcitycategorytype);

//宣告全域性型別陣列

extern sometype const sometypes;

//宣告全域性型別字串函式

extern nsstring * const sometypeidentifier(sometype key);

//定義型別陣列

subcitycategorytype const subcitycategorytypes = ;

//定義字串函式

nsstring * const subcitycategorytypeidentifier(sometype key)

}//傳入列舉值通過字串函式獲取指定字串

subcitycategorytypeidentifier(sometypekey) //使用方式

str = [string jsonstring];

str = [dic jsonstring];

str = [array jsonstring];

iOS 開發之字串處理 1

作為乙個苦逼的碼農 我們每個人都希望不要一直做重複 簡單的 書寫工作 希望我們的專案更加的 模組化 條理化 低耦合 我們都知道,在專案中處理資料 見到最多的也就是字串型別了,那麼關於字串的擷取 替換 編碼等問題就會相繼的出現.我們這時候完全可以封裝出來乙個針對字串處理的類 在裡面新增我們常用到字串處...

字串處理 字串反轉

請原諒博主今天很閒,於是乎博主又開始更新微博了。這次要更新的問題是 編寫乙個函式,反轉乙個單詞的順序。例如 do or do not,there is no try.就要反轉成 try.no is there not,do or do 大家要認真看看這道題,這道題和大家想象的貌似有點不同。首先字串反...

字串處理

uncode與ansi字串轉換 我們使用windows函式multibytetowidechar將多位元組字串轉換成寬字元字串。函式如下 int multibytetowidechar uintcodepage dworddwflags lpcstrlpmultibytestr intcbmulti...