【ios】字符串中正则表达式使用
java:
public static String convertToMacAddress(String unformattedMAC) {
String formattedMAC = "";
char divisionChar = ':';
if (!unformattedMAC.isEmpty()) {
formattedMAC = unformattedMAC.trim();
formattedMAC = formattedMAC.replaceAll("(.{2})", "$1" + divisionChar).substring(0, 17).toUpperCase();
}
return formattedMAC;
}2.ios中
-(NSString*)converToMacAddress:(NSString*)unformattedMAC{
NSMutableString *formattedMAC = [NSMutableString stringWithString:@""];
char divisionChar = ':';
if (!(unformattedMAC.length == 0)) {
//trim
formattedMAC = [unformattedMAC stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSString *str = [NSString stringWithFormat:@"$1%c",divisionChar];
NSRegularExpression *regExp = [[NSRegularExpression alloc] initWithPattern:@"(.{2})" options:NSRegularExpressionCaseInsensitive error:nil];
formattedMAC = [regExp stringByReplacingMatchesInString:formattedMAC options:NSMatchingReportProgress range:NSMakeRange(0, formattedMAC.length) withTemplate:str];
//substring to last one ':'
formattedMAC = [formattedMAC substringWithRange:NSMakeRange(0,formattedMAC.length-1)];
}
return formattedMAC;
}结果:
输入:0095699964c4
输出:00:95:69:99:64:c4
参考:
1.去除空格:http://blog.csdn.net/yangtb2010/article/details/7925546
2.截取字符:http://www.cocoachina.com/bbs/read.php?tid=89685
3.字符串正则方法:http://www.jianshu.com/p/588fcd6ec3aa
4.在线正则测试:http://tool.oschina.net/regex/
5.字符串操作大全:http://blog.csdn.net/newjerryj/article/details/6262893
相关推荐
zhujiangtaotaise 2020-01-01
84407518 2012-07-16
AllenG 2013-07-07
Beatopcoder 2013-07-14
afrtooylsw 2014-05-14
让编程改变世界 2015-03-13
shqhope 2015-04-09
zoutian00 2015-05-12
87214552 2015-10-27
84407518 2016-01-25
89283517 2012-06-19
ObjectiveC 2012-06-14
huangshm 2019-01-14
86981633 2019-06-29
我的iOS王者之路 2019-06-28
好好学习天天 2019-06-28