我有一个像这样的字符串:
var aString = "This is a string \n\n This is the second line of the string\n\n"
Run Code Online (Sandbox Code Playgroud)
textview中的内容如下所示:
This is a string
This is the second line of the string
// 2 extra lines of unnecessary white space
Run Code Online (Sandbox Code Playgroud)
但我希望它看起来像这样:
This is a string
This is the second line of the string
Run Code Online (Sandbox Code Playgroud)
我想从字符串的末尾删除所有"\n"并删除\n,它会重复自身,因此中间没有空格.
理想情况下,我猜测最终结果应该是这样的:
var aString = "This is a string \n This is the second line of the string"
Run Code Online (Sandbox Code Playgroud) 现在,我正在练习Swift语言.
我创建了一个StringHelper类.
它在str属性中保存一个字符串,并通过使用方法返回没有空格的相同字符串.
class StringHelper {
let str:String
init(str:String) {
self.str = str
}
func removeSpace() -> String {
//how to code
}
}
let myStr = StringHelper(str: "hello swift")
myStr.removeSpace()//it should print 'helloswift'
Run Code Online (Sandbox Code Playgroud)
我只想使用Swift语言...而不是NSString方法,因为我只是练习使用Swift.
我想知道的是如何在swift中创建一个带有可本地化文件的符号的字符串,并在之前替换这种表示法.
"welcome" = "Hello %@, Welcome!"
"seeYou" = "Goodbye %@"
"update" = "All your profile data was update, %@"
Run Code Online (Sandbox Code Playgroud)
在另一个文件中:
func showMessage(name : String){
print(welcome,name)
}
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助,
菲利佩
例如,string = I am a #hashtag1 string #hashtag2 Hi!
在Swift 2中,删除主题标签以使字符串变为的理想方法是什么 I am a string Hi!
Hashtags不是固定字符串.它可以是以任何字符串开头的字符串#.这个问题不应该像这个问题一样重复.
我知道我可以回退到NSStringfunction componentsSeparatedByString,所以这可能是一个挑剔,但我喜欢 Swift 的一件事是它是围绕简洁和简短的语法设计的。
我真的希望我能:var parts = myString.characters.split("${")但该功能仅适用于单个字符,而不适用于两个字符的字符串。我什至尝试过,var parts = myString.characters.split { $0 == "${" }但希望使用单个字符作为分隔符,而不是完整的字符串。:(
是否有我缺少的 api 函数,或者我是否需要坚持使用旧的NSString桥接函数?
我想用带下划线的字符串替换一系列空格.例如
"This is a string with a lot of spaces!"
Run Code Online (Sandbox Code Playgroud)
应该成为
"This_is_a_string_with_a_lot_of_spaces!"
Run Code Online (Sandbox Code Playgroud)
这该怎么做?
用于获取特定字符之前的字符串的代码:
let string = "Hello World"
if let range = string.range(of: "World") {
let firstPart = string[string.startIndex..<range.lowerBound]
print(firstPart) // print Hello
}
Run Code Online (Sandbox Code Playgroud)
首先,我有一个程序将Hex float转换为Binary float,我想从Binary string answer中删除所有"0"直到第一个"1".例:
有任何想法吗?
我的用户注册屏幕中有一个名为的文本字段;用户名。
为了避免用户将此字段留空 - 并使他们更容易使用简单地结合他们的名字和姓氏的用户名 - 我目前有一个IBAction触发时“编辑结束”以从全名中获取值小写。
@IBAction func fullNameEditingEnded(_ sender: Any) {
let userName = fullnameTextfield.text?.lowercased()
usernameTextfield.text = userName
}
Run Code Online (Sandbox Code Playgroud)
我想用下划线替换空格。做到这一点的最佳方法是什么?它是 for in 循环还是有更简单的东西?
好吧,所以我将天气数据提取到我的应用程序中,并且为了提取基于位置的api网址,我必须在api中插入城市和简写州(例如,CA,MO,NY等)网址。我能够同时获得城市和速记状态,问题是城市中的任何空间都需要格式化为下划线,例如纽约必须等于New_York。我该怎么做呢?城市名称为字符串格式。我已经在Stack Overflow上看到了答案,但是没有一个是Swift 4。