1. 分開字符串變成數組並遍歷java
//Objective-c NSString *names = @"zhangxueyou,liuddehua,zhangmanyu,guofucheng"; NSArray *arr = [names componentsSeparatedByString:@","]; for(int i=0;i<[arr count];i++){ NSLog(@"%@",[arr objectAtIndex:i]); }
//java String names = "zhangxueyou,liudehua,zhangmanyu,guofucheng"; String[] arr = names.split(","); for(String s:arr){ System.out.println(s); }
2.判斷是否包含數組
//Objective-c NSString *s = @"my name is liudehua."; NSRange range = [s rangeOfString:@"liudehua"]; if(range.length>0){ NSLog(@"this name contain liudehua."); }
//java String s = "this name is liudehua."; if(s.contains("liudehua")){ System.out.println("this name contain liudehua"); }
3.字符串比較app
//Objective-c NSString *s = @"my name is liudehua."; if([s isEqualToString:@"my name is guofucheng."]){ NSLog(@"same"); }
//java String s = "my name is liudehua"; if(s.equals("my name is guofucheng")){ System.out.println("same"); }
4.字符串拼接this
//Objective-c NSMutableString *ss = [NSMutableString stringWithCapacity:50]; for(int i=0;i<100;i++){ if([ss length]>0){ [ss appendString:@","]; } [ss appendFormat:@"%d",i]; } NSLog(@"%@",ss);
//java StringBuffer ss = new StringBuffer(""); for(int i=0;i<100;i++){ if(ss.length()>0){ ss.append(","); } ss.append(i); } System.out.println(ss.toString());