IOS開發 基本型別轉換

2021-07-03 13:22:08 字數 4899 閱讀 9258

資料型別轉換

int i;

nsstring *str;

float f;

char *c;

nsarray *arr;

i = [str intvalue];   //    nsstring——>int

str = [nsstring stringwithformat:@"%i",i];    //    int ——>nsstring

f = [str floatvalue];    //    nsstring——>float

str =[nsstring stringwithformat:@"%f",f];   //    float——>nsstring

str = [nsstring stringwithcstring:c encoding:nsutf8stringencoding];   

//    char *——>nsstring

arr = [nsarray arraywithobjects:@"hello",@"",@"world", nil];   //   

nsdictionary——>nsstring 

即 nsarray 元素拼接字串    最後必須是nil

str=[array componentsjoinedbystring:@","];

data = [nsdata datawithbytes:c length:strlen(c)];   //    nsdata——>char *

str=[[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];     //    nsdata——>nsstring

const char * ch=[str utf8string];    //    nsstring——>char *

const char * cha=[data bytes];    //    nsdata——>char *

nsurl *url=[nsurl urlwithstring:str];     //    nsstring——>nsurl

str=[[url absolutestring]stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];    //   

nsurl——>nsstring

nsdata *data = [str datausingencoding:nsutf8stringencoding];    //    nsstring——>nsdata  

nsstring 屬性化字串

nsstring *string = @"here is some ****** text that includes bold and italics.n";

nsmutableattributedstring *attrstring = [[nsmutableattributedstring alloc] initwithstring:string];

//     建立可變屬性化字串

nsuinteger length = [string length];

uifont *basefont = [uifont systemfontofsize:10];

//    設定基本字型    

[attrstring addattribute:nsfontattributename value:basefont range:nsmakerange(0, length)];

uifont *boldfont = [uifont boldsystemfontofsize:5];

[attrstring addattribute:nsfontattributename value:boldfont range:[string rangeofstring:@"bold"]

];//   用系統字型的粗體來設定粗體,單詞"bold"

uicolor *color = [uicolor redcolor];

[attrstring addattribute:nsforegroundcolorattributename value:color range:[string rangeofstring:@"bold"]];

self.titlelabel.attributedtext=attrstring;

nsstring 字串的前後空格去掉

[str

stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]];    //    nsstring 型別

nsstring 從檔案建立字串

nsstring *path =[[nsbundle mainbundle]pathforresource:@"1" oftype:nil];    //    路徑

nsstring *str=[[nsstring alloc]initwithcontentsoffile:path encoding:nsutf8stringencoding error:nil];

nsstring 字串

判斷兩者內容是否相同以及 值的大小比較

bool b =[str1 compare:str2]== nsorderedsame ; // 

nsorderedsame 判斷是否相同

。相同返回1,不同返回0

bool b =[str1 compare:str2]== nsorderedascending ;

//    

nsorderedascending

按值得大小判斷 str1 小於 str2 返回1 否則返回0;

bool b =[str1 compare:str2]==nsordereddescending ; 

//    nsordereddescending 

按值得大小判斷 str1 大於 str2 返回1 否則返回0;

bool b=[str1 compare:str2 options:nscaseinsensitivesearch]== nsorderedsame;   //

nscaseinsensitivesearch  不區分大小寫的比較

nsstring *str =  [str1 uppercasestring];

//    將字串變為 大寫

nsstring *str =  [str2lowercasestring];

//    將字串變為 小寫

nsstring *str =  [str1 capitalizedstring];

//    將字串的 首字母 變為 大寫

nsstring

查詢字串某處是否包含其它字串,給出位置以及長度

nsstring

*str1 = @"this is a string";

nsstring *str2 = @"str";

nsrange range = [str1 rangeofstring:str2];

nsuinteger location = range.location;    //    str2在str1中的位置

nsuinteger leight =range.length;

//    str2的長度

nsstring *str = [nsstring stringwithformat:@"location:%lu,leight:%lu",location,leight];

nsstring 字串擷取

(從0開始數的)

nsstring *str1 = @"this is a string";

nsstring *str2 = [str1 substringtoindex:5];   //   

從字串的開頭一直擷取到指定的位置,但不包括該位置的字元

nsstring *str3 = [str1 substringfromindex:5];    //   

以指定位置開始(包括指定位置的字元),幷包括之後的全部字元

nsstring *str4 = [str1 substringwithrange

:nsmakerange(2, 5)];   //    按照所給出的位置

(包括指定位置的字元),長度,任意地從字串中擷取子串

nsstring 字串鏈結

(尾部新增)

nsmutablestring *str1 = [[nsmutablestring alloc] initwithstring:@"nsmutablestring"];   //    nsmutablestring 的兩種方式

str2=[nsstring stringwithformat:@"%@asd",str2];     

nsstring 字串替換

nsmutablestring *str = [[nsmutablestring alloc] initwithstring:@"this is a nsmutablestring"];   

[str replacecharactersinrange

:nsmakerange(0, 4)

withstring:@"that"];    //    指定位置的替換

nsstring 字串的首尾判斷

nsstring *string1 = @"nsstringinformation.txt";

[string1 hasprefix:@"nsstring"] == 1 ?  nslog(@"yes") : nslog(@"no");   //    判斷首部

[string1 hassuffix:@"txt"] == 1 ?  nslog(@"yes") : nslog(@"no");   //    判斷尾部

產生隨機數

int i=arc4random() ;    //    例: arc4random() %10 加上範圍 [0,10)

基本型別轉換

1 整型預設int byte a 1 short b 2 int c 3 long d 4 高位元組轉低位元組,低位元組 裝不下 強轉!byte aa byte b short bb short c int cc int d 低位元組轉高位元組,高位元組 裝得下 自轉!long d1 c int c...

基本型別轉換

object 是最終基類,任何形式值都可以轉化成 object形式。基本型別轉換 強制 顯示 轉換和自動 隱式 轉換!裝箱轉換 允許值型別隱式轉化為引用型別 是 強制轉換!轉換方法 1,用 此類是同類之間轉換,內是轉換之後的 資料型別!例如,int i 0 double d 1.23 i int d...

java基本型別轉換

一 int 和 string 互轉 1 int 轉為 string 1 public static string valueof int i 2 int a 9 string s new integer a tostring 2 string 轉為 int 1 int i integer.parsi...