-2

I have a String that has 2 whitespaces and i would like to only remove the last space in the String and also remove the last text. components(separatedBy: " ") splits test where there is a space which i don't wanna do.

Current Text

let teamName = "Aston Villa -1"

I would like it to be like this

teamName = "Aston Villa"
Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
sk123
  • 580
  • 1
  • 8
  • 29
  • @JoakimDanielson `-1` – sk123 May 06 '20 at 11:48
  • This is coming from the backend, so i just can't remove it like that because the text is normally different. All i want to get is the team name. Thats it. I could just split it and add [0] and [1] together but i don't wanna do it that way – sk123 May 06 '20 at 11:51
  • Does this answer your question? [Remove last character from string. Swift language](https://stackoverflow.com/questions/24122288/remove-last-character-from-string-swift-language) – CodeLikeBeaker May 06 '20 at 11:51
  • That was just an example – sk123 May 06 '20 at 11:52
  • You have to be more specific, what can the last number be. Is it always negative, can it be more than one figure, etc. – koen May 06 '20 at 11:55
  • It can be anything. Could be a number of a word. All i need to is to remove the second space in the string – sk123 May 06 '20 at 11:56
  • @sk123, please see the link I posted in the above comment. I believe that will answer your question - Thanks – CodeLikeBeaker May 06 '20 at 11:56
  • You can iterate through all chars of the string. When you have a whitespaces safe the index of it. After that get the last index from the saved values and with replacingCharacters(in: <#T##RangeExpression#>, with: <#T##StringProtocol#>) you can only replace the last whitespace. – Christian May 06 '20 at 11:59
  • That one i just split it and take the first word in the array [0] using `components(separatedBy: " ")` – sk123 May 06 '20 at 12:02
  • There is some uncertainty as to what you are asking exactly so maybe you could update your questions with some more examples, like "Liverpool -1" or "Aston Villa -1 x" if they are relevant. – Joakim Danielson May 06 '20 at 14:03

4 Answers4

2

This is a solution with Regular Expression.

let teamName = "Aston Villa -1"
let trimmedTeamName = teamName.replacingOccurrences(of: "(?:\\s[^\\s]+)$", with: "", options: .regularExpression)

The pattern searches backwards (?:) for a whitespace character (\\s) followed by 1 or more non-whitespace characters ([^\\s]+) at the end of the string ($)


Alternatively you can use range(of with the .backwards option

if let rangeOfLastWhiteSpace = teamName.range(of: " ", options: .backwards) {
    let trimmedTeamName = String(teamName[..<rangeOfLastWhiteSpace.lowerBound])
}
vadian
  • 274,689
  • 30
  • 353
  • 361
0

Split the string into the array ["Aston", "Villa", "-1"]. Drop the last element of the array: ["Aston", "Villa"]. Join the array back together "Aston Villa".

let fixedTeamName = teamName.split(separator: " ")
    .dropLast()
    .joined(separator: " ")

Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
0
let kutcer = "Aston Villa -1"
kutcer.lastIndex(of: " ").map( kutcer.prefix(upTo:) ) == "Aston Villa" // true

…and if you need it as a non-optional String:

kutcer
.lastIndex(of: " ")
.map( kutcer.prefix(upTo:) )
.map(String.init)
?? kutcer
-2

This should work, also for one word team names.

var teamName = "Aston Villa -1"

var foundSpace = false

while foundSpace == false {
    if teamName.removeLast() == " " {
        foundSpace = true
    }
}

print(teamName) //  "Aston Villa"
koen
  • 5,383
  • 7
  • 50
  • 89