-3

I'm working on swift. How to delete between 2 characters, for example from '[' to ']' and how to delete "glass" word from a string?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
marmaralone
  • 516
  • 6
  • 11
  • There's lots of techniques for scanning strings in [this NSHipster article](http://nshipster.com/nsscanner/). – Eric Aya Apr 13 '15 at 14:55
  • Because of my Reputation is not enough to ask my question in more than 2 lines, i had to shrink my question and you're absolutely right that its little bit hard to understand what i try to ask. Sorry about that :/ – marmaralone Apr 16 '15 at 01:22

2 Answers2

0

You might want to try using subStringWithRange

var str = "Hello, playground"
var startIndex = 0
var endIndex = 18
str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex)) //"Hello, playground"

Then, you could assign that to a variable and do the rest. If you need further assistance let me know. Credit goes here. You can also have a look there for support.

Hope that helps :)

Community
  • 1
  • 1
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
  • Thank you my friend :) My question is not well-explained because of my lask Reputation score, sorry about that. I was trying to ask if start and end indexes are not certain, it changes on every query. – marmaralone Apr 16 '15 at 01:23
0

Here is a function modified to allow multiple deletions. The first function handles a single deletion. The second function splits the sentence into different sentences one for each start-end group and then applies function 1 before concatenating to get the final result. I have not tested it exhaustively. the first function is protected for non-existent startString and/or endString.

Function deleteEnclosedString returns the string remaining after string enclosed within startString and endString (inclusive) is deleted.

    import UIKit
    import Foundation


    var str = "Hello, [play]ground [again]hi everybody [again]"


func deleteEnclosedString (s: String, s1:String, s2: String) -> String {
    var returnedString = s
    var x = s.componentsSeparatedByString(s1)
    if (x[0] != s){ // startString present
        let y = x[1].componentsSeparatedByString(s2)
        if  (y[0] != x[1]) { // start and endString present
            returnedString = x[0]+y[1]
        } else { // startString present, endString missing
            returnedString = x[0]
        }
    } else { // startString missing
        let y = s.componentsSeparatedByString(s2)
        if  (y[0] != s)  { // no startString, endString present
            returnedString = y[1]
        } else { // no startString, endString missing
            returnedString = y[0]
        }
    }
    //println("returnedString: "+returnedString)
    return returnedString

}

func deleteAllEnclosedStrings (s: String, startString s1:String, endString s2:String) -> String{

    var returnedString = ""
    var allSubstrings = s.componentsSeparatedByString(s1)
    var strCount = allSubstrings.count
    for i in (0 ..< strCount) {
        var y = deleteEnclosedString(allSubstrings[i], s1 , s2)
        returnedString = returnedString + y
    }
    return(returnedString)
}

// test cases
// start and end available
deleteAllEnclosedStrings(str, startString: "[", endString: "]") // Hello, ground hi everybody
// start but no end
deleteAllEnclosedStrings(str, startString: "[", endString: "}") // Hello, play]ground again]hi everybody again] and 
Syed Tariq
  • 2,878
  • 3
  • 27
  • 37
  • This is exactly what i'm looking for my friend. I appriciate for your help, thank you. But in this case, i need to change something on this code, because i tried it and it works well if i have only one start and end points. If i have multiple, it doesnt work. For example this string var str = "Hello, [play]ground [again]hi everybody [again]" when i run your code on this string, i get something like this = "Hello, ground" but i have to clean up everything between '[' and ']' and i have to get this result = "Hello, ground hi everybody" I hope i could explain myself better in this comment – marmaralone Apr 16 '15 at 01:25
  • solved it, than you ;) – marmaralone Apr 17 '15 at 00:21
  • I was in the middle of an update and may have over-written your response. Sorry. I am curious what your solution was. – Syed Tariq Apr 17 '15 at 00:53
  • `var startString = "[" var endString = "]" while (c.rangeOfString(startString) != nil){ var x = c.componentsSeparatedByString(startString) if (x[0] != c){ var y = x[1].componentsSeparatedByString(endString) c = c.stringByReplacingOccurrencesOfString(y[0], withString:"") //delete inside of startString and endString c = c.stringByReplacingOccurrencesOfString(startString + endString, withString:"") //delete the startString and endString characters } }` PS: I still couldn't get used to this answer format :/ – marmaralone Apr 17 '15 at 14:00