關於NSString的練習題

2021-07-07 05:20:03 字數 2715 閱讀 9074



//1.現有如下定義的字串: nsmutablestring * str

=@「iphoneandroid」,能不能對該字串進行修改,如果能,請輸出刪除android後的新字串。

nsmutablestring *str=[nsmutablestring stringwithcapacity:2];//建立乙個可變的字串 

nsrange r=;//擷取字串 

[str deletecharactersinrange:r];//刪除字串 

nslog(@"new str string id=%@",str);//輸出字串 

第二種方法

nsmutablestring *str=[[nsmutablestring alloc]initwithstring:@"iphoneandroid"];//建立可變字串並賦值

[str deletecharactersinrange:nsmakerange(6, 7)];//刪除從下標是6的位置,長度是7的字串

nslog(@"%@",str);

//2求字串「158」和「39」按十進位制數值做差後的結果以字串形式輸出 

nsmutablestring *str1=[nsstring stringwithformat:@"%@",@"158"];//定義字串 

nsmutablestring *str2=[nsstring stringwithformat:@"%@",@"39"]; 

int a=[str1 intvalue];//把字串轉換成 int型 

int b=[str2 intvalue]; 

nsmutablestring *str3=[nsmutablestring stringwithformat:@"%d",(a-b)];//(a-b)結果 在格式化成字串賦給str3 

nslog(@"results:%@",str3); 

第二種方法

nsmutablestring *str1=[[nsmutablestring alloc]initwithstring:@"158"];

nsmutablestring *str2=[[nsmutablestring alloc]initwithstring:@"39"];

int a=[str1 intvalue];

int b=[str2 intvalue];

nsmutablestring *str3=[nsmutablestring stringwithformat:@"%d",(a-b)];

nslog(@"%@",str3);

第一種方法 

nsmutablestring *str4=[nsmutablestring stringwithcapacity:0]; 

nsrange r1=; 

nsrange r2=; 

nsrange r3=; 

nsstring*str5=[str4 substringwithrange:r1]; 

nsstring*str6=[str4 substringwithrange:r2]; 

nsstring*str7=[str4 substringwithrange:r3]; 

nsmutablestring *str8=[nsmutablestring stringwithcapacity:0]; 

nslog(@"%@",str8);

//第二種 方法 

nsstring *str5=@"123-456-789-000"; 

nsarray *newarray=[str5 componentsseparatedbystring:@"-"];//用什麼 分開 

nslog(@"%@",newarray); 

nsmutablestring *newstr=[nsmutablestring stringwithcapacity:0]; 

for (nsstring *s in newarray)// s是指向元素 

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

//第三種方法 

nsstring *str5=@"123-456-789-000";

nsstring *newstr5= 

[str5 stringbyreplacingoccurrencesofstring:@"-" withstring:@""];//替換法 把-替換成空

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

//4放四個十進位制三位數到乙個陣列中,然後按從小到大排序後組成乙個新的陣列

nsmutablearray *numarray=[[nsmutablearray alloc]initwithobjects:@"114",@"159",@"210",@"999", nil]; 

for (int i=0; i}  } 

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

第二種方法

nsmutablearray *numarray=[[nsmutablearray alloc]initwithobjects:@"114",@"159",@"210",@"999", nil];

for (int i=0; i<[numarray count]; i++)//冒泡法比較

} }nslog(@"numarray =%@",numarray);

關於陣列的練習題

題目描述 從鍵盤讀入學生成績,找出最高分,並輸出學生成績等級。成績 最高分 10 等級為 a 成績 最高分 20 等級為 b 成績 最高分 30 等級為 c 其餘 等級為 d public class arraydemo1 5.根據每個學生成績與最高分的差值,得到每個學生的等級,並輸出等級和成績 c...

python的練習題 Python練習題

1 使用while迴圈輸入1 2 3 4 5 6 8 9 10 i 0while i 10 i i 1 if i 7 continue print i 結果 e python python python test.py1 2 求1 100的所有數的和 i 0sum 0 while i 100 i 1...

java關於switch case的練習題

1.若a和b均是整型變數並已正確賦值,正確的switch語句是 a switch a b b switch a b 3.0 c switch a d switch a b 2.設int 型變數 a b,float 型變數 x y,char 型變數 ch 均已正確定義並賦值,正確的switch語句是 ...