简体   繁体   English

如何在特定字母后将一个字符串插入另一个字符串?

[英]How do I insert a string into another string after a specific letter?

I'm storing heights in my database as 62 or 511 instead of 6 ft 2 or 5 ft 11. 我在数据库中将高度存储为62或511,而不是6 ft 2或5 ft 11。

This is how I prepare the string that will be saved: 这是我准备将要保存的字符串的方法:

    let height = 6 ft 2
    let myNewString = height.stringByReplacingOccurrencesOfString(" ft ", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
    println(myNewString) // 62

What I'd like to do is now do the opposite thing when displaying back the data from the database. 我现在想做的是在显示数据库中的数据时做相反的事情。

So I'd like to take the returned "62" from the database and insert this string " ft " into it. 因此,我想从数据库中获取返回的“ 62”并将此字符串“ ft”插入其中。 My aim is to end up with "6 ft 2" to display on the app. 我的目标是最终显示“ 6 ft 2”以显示在应用程序上。

How can I do this? 我怎样才能做到这一点?

I want to avoid storing two columns dedicated to height in my database. 我想避免在数据库中存储两个专门用于高度的列。 I only know how to concatenate strings but this isn't exactly concatenating a string. 我只知道如何串联字符串,但这并不完全是串联字符串。 I am inserting it into an existing string. 我将其插入到现有字符串中。

I'd appreciate an efficient way to do this. 我希望能有一种有效的方法来做到这一点。 Been looking around for a while now and can't seem to find my solution. 现在已经四处寻找,似乎找不到我的解决方案。

Thanks for your time. 谢谢你的时间。

Update: 更新:

After reading comments I'm wondering if it would be better to add a decimal point after the first char in string eg 4.2, 4.10, 5.4, 5.11, 6.2 etc 阅读评论后,我想知道在字符串中的第一个字符之后添加小数点是否更好,例如4.2、4.10、5.4、5.11、6.2等

let height = 6 ft 2
let myNewString = height.stringByReplacingOccurrencesOfString(" ft ", withString: ".", options: NSStringCompareOptions.LiteralSearch, range: nil)
println(myNewString) // 6.2

It would probably be a better idea to store the height purely in inches (eg store 74 for 6'2"). 最好仅以英寸为单位存储高度(例如,存储74英寸为6'2“)。

Anyway: 无论如何:

    let input = "62"
    let feet = (input as NSString).substringToIndex(1)
    let inches = (input as NSString).substringFromIndex(1)
    let output = "\(feet) ft \(inches)"

If you don't like the use of NSString , you can avoid it this way: 如果您不喜欢使用NSString ,可以这样避免:

    let input = "62";
    let feet = input.substringToIndex(input.startIndex.successor())
    let inches = input.substringFromIndex(input.startIndex.successor())
    let output = "\(feet) ft \(inches)"

Swift's String conforms to RangeReplaceableCollectionType , which has a splice method – splice allows you to insert a collection into another collection. Swift的String符合RangeReplaceableCollectionType ,它具有一个splice方法– splice允许您将一个集合插入另一个集合。 Since strings are collections, that means you can write: 由于字符串是集合,因此您可以编写:

var height = "62"
if !height.isEmpty {
    height.splice(" ft ", atIndex: height.startIndex.successor())
} // height is now "6 ft 2"

Alternatively if you know you only want the first character before the “ft” you can do: 或者,如果您知道只希望在“ ft”之前的第一个字符,则可以执行以下操作:

let height = "62"
if let feet = first(height) {
    let inches = dropFirst(height)
    let heightWithFt = "\(feet) ft \(inches)"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM