0

I'm creating an app that removes new lines and time stamps from a huge multi-line String given. I managed to remove the new lines by using string.replacingOccurences(of:_, by:_), but couldn't finc a way to get rid of the time stamp. Here's a snippet of a sample string:

6:08phones and laptops connected to the free
6:10public Wi-Fi network due to this she
6:12could hack email accounts online bank
6:14accounts and other passwords however she
6:17had no deep knowledge of computers or
6:19hacking so how did she do this
6:21well she simply watched an 11 minute
6:23YouTube video on how to do it she was
6:25later hired by a VPN website which tests
6:27security that's one way to land a job
6:30and that just about wraps up this video
6:32check out the poll in the top right
6:34corner
6:35you guys can vote for the most
6:36surprising hacker but as always thanks
6:38for watching check out some more videos
6:40on screen right now leave a like if you
6:42enjoyed and if you haven't already what
6:44are you waiting for subscribe 
Fayyouz
  • 682
  • 7
  • 18

2 Answers2

0

Try below code for remove only time stamp from string.

(Time stamp must be in this format only. If format changes then it can be issue occurs.)

let string = "6:42enjoyed and if you haven't already what"
let result = string.trimmingCharacters(in: CharacterSet(charactersIn: "01234567890:"))
print(result)

console prints:-
enjoyed and if you haven't already what

Shah Nilay
  • 778
  • 3
  • 21
  • 2
    Wouldn't that remove revery single number in the string? – Fayyouz Aug 12 '17 at 12:15
  • working in my project. If you have issue in your code then please paste your code in question. – Shah Nilay Aug 12 '17 at 12:17
  • This actually works on a single line. I need something that works on all my multi-line String (the string entered would be 35 lines) – Fayyouz Aug 12 '17 at 12:24
  • when you are remove newline at the same next statement you have to use above code for remove time stamp. Then it will be working fine. – Shah Nilay Aug 12 '17 at 12:26
  • if you don't mind then please paste your whole string with 35 lines in question. also put your code which removes new line from string. – Shah Nilay Aug 12 '17 at 12:28
  • Okay just 1 second, i finally figured it out – Fayyouz Aug 12 '17 at 12:35
  • Okay, I edited the question to show a bigger snippet of the string, then posted the answer. It works perfectly, please check it – Fayyouz Aug 12 '17 at 12:41
  • The issue is solved completely, still a tiny performance problem, but it's not a big deal – Fayyouz Aug 12 '17 at 12:57
0

You test each character of the string for being a colon, then its following and preceding characters for being numbers, and remove them, as following:

func cleanText(_ text: String) -> String {
    var myText = text.replacingOccurrences(of: "\n", with: " ")
    for i in (1..<myText.characters.count-1).reversed() {

        let i0 = myText.index(myText.startIndex, offsetBy: i)
        if myText[i0] == ":" {
            let i1 = myText.index(myText.startIndex, offsetBy: i+1)
            guard let _ = Int(String(myText[i1])) else { break }

            let i01 = myText.index(myText.startIndex, offsetBy: i-1)
            guard let _ = Int(String(myText[i01])) else { break }

            let i2 = myText.index(myText.startIndex, offsetBy: i+2)
            guard let _ = Int(String(myText[i2])) else { break }

            myText.remove(at: i2)
            myText.remove(at: i1)
            myText.remove(at: i0)
            myText.remove(at: i01)
        }
    }
    return myText
}
Fayyouz
  • 682
  • 7
  • 18