美文网首页
NSString & String

NSString & String

作者: 学游泳的小黑 | 来源:发表于2017-12-26 09:38 被阅读20次

    1、NSURL-->String
    NSString *urlString = url.absoluteString
    NSString *str = @"www.weixin.com";
    NSURL *url = [];
    string -> url
    NSURL *URL = [NSURL URLWithString:str];

    2、保留两位小数点
    String(format: "%.2f", self.resultData[indexPath.row].costPrice)

    3、保留小数点--千分位转换
    // 保留小数点后两位
    let string = "1991.211218"
    let string2 = String(formater: "%.2f", string)
    print(string2) === 1991.21
    // 添加千分位标识符
    func separatedFloatStrWith(string: NSString, precision: Int) -> String {
    let formatter = NSNumberFormatter()
    let value = string.doubleValue
    var format = NSMutableString(string: "###,##0")
    if precision == 0 {
    formatter.positiveFormat = format as String
    return formatter.stringFromNumber(NSNumber(double: value))!
    }else{
    format = NSMutableString(string: "###,##0.")
    for _ in 1...precision {
    format.appendFormat("0")
    }
    formatter.positiveFormat = format as String
    return formatter.stringFromNumer(NSNumber(double: value))!
    }
    }

    4、拼接
    NSString *newString = [NSString stringWithFormat:@"%@%@", tempA, tempB];

    5、字符转int
    int intString = [newString intValue];
    int转字符
    NSString *stringInt = [NSString stringWithFormat:@"%d", intString];

    7、字符转float
    float floatString = [newString floatValue];
    float转字符
    NSString *stringFloat = [NSString stringWithFormat:@"%f", intString];

    8、set方法
    set {
    let startIndex = self.startIndex.advancedBy(range.startIndex)
    let endIndex = self.startIndex.advancedBy(range.endIndex)
    let strRange = Range(startIndex..<endIndex)
    self.replaceRange(strRange, with: newValue)
    }

    9、
    Swift学习之转义字符

    相关文章

      网友评论

          本文标题:NSString & String

          本文链接:https://www.haomeiwen.com/subject/rnhxgttx.html