我有一堆地址作为字符串,采用以下示例格式:
8 Smith st,Sydney,New South Wales,Australia
但是,我想将它们修改为以下格式:
8 Smith st,Sydney
我怎样才能实现这一目标?感谢。
答案 0 :(得分:2)
Scanner scanner = new Scanner(new File("D:\\myFolder\\myfile.csv"));
在你的Case中,首先你必须在Array中转换它然后将它转换为字符串,如下面的例子所示
var myString = " Let's trim the whitespace "
var newString = myString.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
//Returns "Let's trim the whitespace"
答案 1 :(得分:1)
在你的Case中,首先你必须在Array中转换它然后将它转换为字符串,如下面的例子所示
var myString = "8 Smith st, Sydney, New South Wales, Australia"
var myArray = myString.componentsSeparatedByString(",")
//Returns an array with the following values: ["8 Smith st", " Sydney", " New South Wales", " Australia"]
if myArray.count > 1
{
println(myArray[0]) //8 Smith st
println(myArray[1]) //Sydney
}
答案 2 :(得分:0)
将字符串/文字截断为特定长度
如果您输入了句子/文本块,并且只想从文本中保存指定的长度。将以下扩展名添加到Class
extension String {
func trunc(_ length: Int) -> String {
if self.characters.count > length {
return self.substring(to: self.characters.index(self.startIndex, offsetBy: length))
} else {
return self
}
}
}
使用
var str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry."
//str is length 74
print(str)
//O/P: Lorem Ipsum is simply dummy text of the printing and typesetting industry.
str = str.trunc(40)
print(str)
//O/P: Lorem Ipsum is simply dummy text of the