Reputation: 2418
Please could somebody help me.
I am trying to add a new line (\n) into an existing string.
Lets say the string is 20+ Characters long, I want to find a space " " between the range of 15 and 20 then inset a new line (\n) just after the index to where the char " " (space) is
I hope that makes sense :F
Code i have for this so far is as follows
var newString = string
newString[newString.startIndex..< newString.startIndex.advancedBy(16)]
/* let startIndex = newString.startIndex.advancedBy(16)
let endIndex = newString.endIndex
let newRange = startIndex ..< endIndex
print("start index = \(newRange)")*/
let range: Range<String.Index> = newString.rangeOfString(" ")!
let index: Int = newString.startIndex.distanceTo(range.startIndex)
newString.insert("\n", atIndex: newString.startIndex.advancedBy(index))
label.text = newString
if I try the following
let newIndex = name.startIndex.advancedBy(19).distanceTo(range.endIndex)
I get the error message
fatal error: can not increment endIndex
Ive got a feeling I'm on the right tracks but the above will inset a new line at the index where space first appears in the string and not between the index of e.g. 15 and 20
Thanks for your help in advance
Thomas
Upvotes: 0
Views: 663
Reputation: 2418
Thanks to dfri for pointing me in the right direction. Although his answer was correct for my problem specifically I've provided the following code to help
if string.characters.count >= 20
{
let bar = 15 // where to split the code to begin looking for the character
let beginString = string.substringWithRange(name.startIndex..<name.startIndex.advancedBy(bar))
var endString = string[string.startIndex.advancedBy(bar)..<name.endIndex]
if endString.containsString(" ")
{
let range = endString.rangeOfString(" ")
if let i = range
{
endString.insert("\n", atIndex:i.startIndex.advancedBy(1) )
let newString = "\(beginString)\(endString)"
label.text = newString
}
}
}
Upvotes: 0
Reputation: 73206
The following finds the first space in the range 15..<END_OF_YOUR_STRING
, and replaces it with a new line (\n
). In your question you stated you explicitly wanted to look for a space in range 15...20
, and also insert a new line after the space. Below I have assumed that you actually want:
15
, but continuing until you find one (otherwise: if you find no space within range 15...20
, no line break should be inserted?).Both of these deviations from your question can be quite easily reverted, so tell me if you'd prefer me to follow your instructions to specifically to the point (rather than including my own reason), and I'll update this answer.
Solution as follows:
var foo = "This is my somewhat long test string"
let bar = 15 /* find first space " " starting from index 'bar' */
if let replaceAtIndex = foo[foo.startIndex.advancedBy(bar)..<foo.endIndex]
.rangeOfString(" ")?.startIndex.advancedBy(bar) {
foo = foo.stringByReplacingCharactersInRange(
replaceAtIndex...replaceAtIndex, withString: "\n")
}
print(foo)
/* This is my somewhat
long test string */
Note that there is a off-by-one difference between finding a space in the range of 15 to 20 and the 15:th to 20:th character (the latter is in the range 14...19
). Above, we search for the first space starting at the 16th character (index 15
).
Upvotes: 1