如何通过删除&QUOT改变我的字符串值到数组;()" [英] how to change my String Value into Array by removing "()"
本文介绍了如何通过删除&QUOT改变我的字符串值到数组;()"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让说我有这样的价值观
苹果(100)
橙色(300)
菠萝(10)
葡萄(50)
香蕉(1000)
我想要做的就是创建一个数组,就像是给每个
What I want to do is to create an array which was like that to each string
[苹果,100]
[橙色,300]
我的尝试是这样的,但它不符合我的回答不够好
What i tried was like that,but it doesn't meet my answer well enough
var myNewFruits = "Apple(200)"
var newStr = myNewFruits.componentsSeparatedByString("(")
产量为
["Apple","200)"]
我真正想要的是
["Apple","200"]
是否与斯威夫特帮助吗?谢谢你。因为我创建与搜索,所以,我真的需要它。
Is there any help with Swift?Thank you.Because I am creating search with that,so,i really need it.
推荐答案
您可以使用自定义 NSCharacterSet
,并从返回的数组前两个元素:
You can use a custom NSCharacterSet
and get the first two elements from the returned array:
let myNewFruits = "Apple(200)"
let newStr = myNewFruits.componentsSeparatedByCharactersInSet(NSCharacterSet(charactersInString: "()"))[0...1] // ["Apple", "200"]
这篇关于如何通过删除&QUOT改变我的字符串值到数组;()"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文