39

What's the best way to go about removing the first six characters of a string? Through Stack Overflow, I've found a couple of ways that were supposed to be solutions but I noticed an error with them. For instance,

extension String {
func removing(charactersOf string: String) -> String {
    let characterSet = CharacterSet(charactersIn: string)
    let components = self.components(separatedBy: characterSet)
    return components.joined(separator: "")
}

If I type in a website like https://www.example.com, and store it as a variable named website, then type in the following

website.removing(charactersOf: "https://")

it removes the https:// portion but it also removes all h's, all t's, :'s, etc. from the text.

How can I just delete the first characters?

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
Vandal
  • 708
  • 1
  • 8
  • 21
  • Use `if str.length >= 6 { let newstr = str.chopPrefix(6) }`. See http://stackoverflow.com/a/30829999/1630618 – vacawama Apr 13 '17 at 10:14

4 Answers4

57

In Swift 4 it is really simple, just use dropFirst(n: Int)

let myString = "Hello World"
myString.dropFirst(6)
//World

In your case: website.dropFirst(6)

Gefilte Fish
  • 1,600
  • 1
  • 18
  • 17
34

Why not :

let stripped = String(website.characters.dropFirst(6))

Seems more concise and straightforward to me.

(it won't work with multi-char emojis either mind you)

[EDIT] Swift 4 made this even shorter:

let stripped = String(website.dropFirst(6))
Alain T.
  • 40,517
  • 4
  • 31
  • 51
15

length is the number of characters you want to remove (6 in your case)

extension String {

  func toLengthOf(length:Int) -> String {
            if length <= 0 {
                return self
            } else if let to = self.index(self.startIndex, offsetBy: length, limitedBy: self.endIndex) {
                return self.substring(from: to)

            } else {
                return ""
            }
        }
}

enter image description here enter image description here

Neethu M
  • 644
  • 6
  • 11
  • 2
    have you tried emojis? "‍‍‍".toLengthOf(length: 1) will be "‍" – Kubba Apr 13 '17 at 08:59
  • @Kubba This works for emojis as well. In your case, the output would be "". – Neethu M Apr 13 '17 at 09:06
  • after your edits - now it outputs: `"12".toLengthOf(length: 1) // 2`, `"‍‍‍".toLengthOf(length: 1) // "‍‍"` – Kubba Apr 13 '17 at 09:13
  • @Kubba I have added the screen shot of your scenario. Please have a look. – Neethu M Apr 13 '17 at 09:26
  • use emoji which is made from more than emoji. For example is made from one emoji. ‍‍‍ is made from four basic emojis. `"\u{200D}\u{200D}\u{200D}" == "‍‍‍" //true` – Kubba Apr 13 '17 at 09:30
  • because is a single emoji, ‍‍‍ is not, try with it. – Kubba Apr 13 '17 at 09:43
  • 1
    @Kubba This won't work for emojis made from more than one emojis because a normal emoji would contain only one character whereas the one you mentioned contains multiples characters. But I think it will work for the scenario asked in the question. – Neethu M Apr 13 '17 at 10:01
  • Getting a warning as 'substring(from:) is deprecated.' – Jayprakash Dubey Mar 08 '19 at 06:13
0

It will remove first 6 characters from a string

    var str = "Hello-World"
    let range1 = str.characters.index(str.startIndex, offsetBy: 6)..<str.endIndex
    str = str[range1]
    print("the end time is : \(str)")
srinivasg
  • 1
  • 1