4 swift 字符串转数子_swift判断字符串包含子字符串 - CSDN
  • 字符串转数组 let str = "1,2,3" let strArray = str.componentsSeparatedByString(",") 数组转字符串 let array = ["1","2","3"] let arrayStr = ",".join(array) println(arrayStr) //"1,2,3" //如果只是...

    字符串转数组

    let str = "1,2,3"
    let strArray = str.componentsSeparatedByString(",")


    数组转字符串

    let array = ["1","2","3"]
    let arrayStr = ",".join(array)
    println(arrayStr) //"1,2,3"
    
    //如果只是想打印出来可以这么用
    println(array.description) //"[1, 2, 3]"


    展开全文
  • Swift中,如果需要把一个字符串根据特定的分隔符拆分(split)成字符串数组,通常有如下两种方法: 1,使用componentsSeparatedByString()方法 1 2 3 4 5 let str = "北京、上海、深圳、香港&...

    在Swift中,如果需要把一个字符串根据特定的分隔符拆分(split)成字符串数组,通常有如下两种方法:


    1,使用componentsSeparatedByString()方法
    1
    2
    3
    4
    5
    let str = "北京、上海、深圳、香港"
    print("原始字符串:\(str)")
     
    let splitedArray = str.componentsSeparatedByString("、")
    print("拆分后的数组:\(splitedArray)")
    原文:Swift - 将字符串拆分成数组(把一个字符串分割成字符串数组)



    2,使用characters.split()方法

    1
    2
    3
    4
    5
    let str = "北京、上海、深圳、香港"
    print("原始字符串:\(str)")
     
    let splitedArray = str.characters.split{$0 == "、"}.map(String.init)
    print("拆分后的数组:\(splitedArray)")
    展开全文
  • 字符串转为数组:  let str = "xiaoming,xiaohong,xiaoluo"  let strArray = str.componentsSeparatedByString(",") 数组转为字符串:  let strArray = ["xiaoming","xiaohong","xiaolu

    字符串转为数组:

                    let str = "xiaoming,xiaohong,xiaoluo"

                        let strArray = str.compontsSeparatedByString(",") 


    数组转为字符串:

           let strArray = ["xiaoming","xiaohong","xiaoluo"]

           let str = strArray.joinWithSeparator(",")

                 

    展开全文
  • class func stringToArray(input:NSString) ->NSArray { let arrInput = (0..<input.length).map({ Character(UnicodeScalar(input.character(at: $0))!) }) return...

     class func stringToArray(input:NSString) ->NSArray {

            let arrInput = (0..<input.length).map({

                Character(UnicodeScalar(input.character(at: $0))!)

            })

            return arrInput as NSArray

        }

    or

     class func stringToArray(input:String) ->Array {

             return  input.map { String($0) }

        }

     

    转载于:https://www.cnblogs.com/akiha/p/9897301.html

    展开全文
  • 1.截取前面三个字符串,要求输出结果为abc let str = "abcdefghi" let index = str.index(str.startIndex, offsetBy: 3)//获取字符d的索引 let result = str.substring(to: index)//从起始截取到索引的所有字符串...

    1.截取前面三个字符串,要求输出结果为abc

    let str ="abcdefghi"

    let index = str.index(str.startIndex, offsetBy:3)//获取字符d的索引

    let result = str.substring(to: index)//从起始截取到索引的所有字符串,即abc,注意不包含d

    打印结果为 abc


    2.截取第四个开始的所有字符串,要求输出结果为defghi

    let str = "abcdefghi"

    let index = str.index(str.startIndex, offsetBy: 3)//获取字符d的索引

    let result = str.substring(from: index)//从d的索引开始截取后面所有的字符串即defghi

    打印结果为defghi


    3.截取第4个开始的长度为2个字符串,要求结果输出为de

    let str ="abcdefghi"

    let startIndex = str.index(str.startIndex, offsetBy:3)//获取d的索引

    let endIndex = str.index(startIndex, offsetBy:2)//从d的索引开始往后两个,即获取f的索引

    let result = str.substring(with: startIndex..<endIndex)//输出d的索引到f索引的范围,注意..<符号表示输出不包含f

    打印结果为de


    4.判断字符串是否包含某段字符

    let str = "abcdefghi"

    let result = str.contains("ac")

    打印结果为false

    let result = str.contains("abc")

    打印结果为true


    5.字符串大小写转换

    let str = "ABCDEFGHI"

    let result = str.lowercased()

    输出结果为abcdefghi


    let str = "abcdefghi"

    let result = str.uppercased()

    输出结果为ABCDEFGHI


    6.指定字符串替换为其他字符串,将abc替换成m,使输出结果为mdefghi

    let str = "abcdefghi"

    let result = str.replacingOccurrences(of:"abc", with: "m")

    输出结果为mdefghi


    7.指定范围内的字符串替换为其他字符串,将cde替换成m,使输出结果为abmfghi

    let str = "abcdefghi"

    let startIndex = str.index(str.startIndex, offsetBy:2)

    let endIndex = str.index(startIndex, offsetBy:3)

    let result = str.replacingCharacters(in: startIndex..<endIndex, with:"m")

    输出结果为abmfghi


    8.有时候不确定字符串的长度,需求为截取字符串末尾面开始往前5位,截取长度为2的字符串,使输出为ef

    let str = "abcdefghi"

    let startIndex = str.index(str.startIndex, offsetBy: str.lengthOfBytes(using: .utf8) - 5)

    let endIndex = str.index(startIndex, offsetBy:2)

    let result = str.substring(with: startIndex..<endIndex)

    输出结果为ef


    9. 字符串转为数组,使得输出为["a", "b", "c", "d", "e", "f", "g", "h", "i"]

    let str = "a.b.c.d.e.f.g.h.i"

    let result = str.components(separatedBy:".")

    输出为["a", "b", "c", "d", "e", "f", "g", "h", "i"]




















    展开全文
  • 字符串String1.声明// 空字符串 let emptyString = "" let anatherString = String() if emptyString.isEmpty { print("空的,我擦") }2.转义字符/// 转义字符\0(空字符)、\\(反斜线)、\t(水平制表符)、\n(换行符)...
  • NSArray *arrays = ...@[@"1,2,3,4,5,6"];   NSString *string = [arrays componentsJoinedByString:@","];  NSArray *array = [string componentsSeparatedByString:@","];  NSLog(@"=string=---
  • 字符串转数组 筛选字符串内部的数组 let hstring = “1j2k3jj34” let wan = hstring.characters .map{ Int(String($0)) } .filter{ $0 != nil } print(wan)
  • 1、JSONString转换为字典 // JSONString转换为字典 func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{ ... let jsonData:Data = jsonString.data(using: .utf8)!... ...
  • // 字符串的定义 // 1 使用字符串字面量 var name = "DevZhang" print(name) // 2 String 实例化 name = String("Hello, DevZhang") print(name)// 字符串追加字符串 name = name + ".where are you from?" print...
  • 字符串转数组;字符串数组转字符串;字符串数组转long数组
  • 方法1var Student = ("中华人民",20,"男",175.9) var str = "\(Student.0)\(Student.1)\(Student.2)\(Student.3)" print(str)方法2 var Student = ("中华人民",20,"男",175.9) var str2 = "" let (name,age,sex,...
  • //数组转JSON func arrayToJson(_ arr...
  • 字符串通过length()方法: str.length() 数组通过length属性: arr.length  集合通过size()方法: list.size()
  • 解析报文中经常会碰到将机器码十六进制字符串转成字节数组,下面的方法挺好用 2、使用 private static String hexStr = "3131303236303832393743323437333530303231340000000000000000"; public ...
  • **split():**根据分隔符、将字符串拆分成数组。 注1:方法括号里面可以是双引号也可以是单引号,这是必须项。如果括号里什么都没有,字符串是按原样存放在一个数组里。 var str = "abcdefg"; var arr = str.split('...
  • IOS 如何把16进制字符串转换成byte?求各位大神 最好能有代码
  • swift 字符串小记

    2014-11-26 04:08:55
    字符串分割函数: let str:String = "http://www.example.com/img/string.jpg" imgback(str) imgpath(str) func imgback(str:String) -> String {  var imgname:NSString = str.lastPathComponent ...
  • 通过String的characters方法,将String转Character数组 例如:  let characters:Array<Character> = Array("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRST".characters) ...
  • import java.util.ArrayList; public class Try { /** * @去掉重复值*/ public static void main(String[] args) { String test="100,120,166,1555,120,150,100"; String[] test1=test.split("...
1 2 3 4
收藏数 66
精华内容 26
关键字:

4 swift 字符串转数子