shubham mishra
shubham mishra

Reputation: 991

Insert character Just after particular character in string : Swift

I have a Url string and i want to insert minus "-" just after equal to sign(=). I have following string :-

http://www.corpebl.com/blog.php?show=250

or

http://www.bdm.com/gm.php?id=2

And i want the following string :-

http://www.corpebl.com/blog.php?show=-250

or

http://www.bdm.com/gm.php?id=-2

Please try to avoid to use index count because length is variable every time so position of equal to(=) will also be different every time.

Upvotes: 0

Views: 977

Answers (4)

pbodsk
pbodsk

Reputation: 6876

I know that you have a working solution now but I thought I'd chime in with something a bit more general.

If you want more control over which parameters to append a minus sign to, you should look into the NSURLQueryItem class (documented here).

You separate the URL into components by using the NSURLComponents class (documented here) and can then get access to the queryItems array.

So something like:

if let components = NSURLComponents(string: "http://www.corpebl.com/blog.php?show=250"),
   let queryItems = components.queryItems {

}

Will give you a URL, split into components from your string and you have the query items in an array. Now you are ready to look at the individual queryItems:

for item in queryItems where item.value != nil {
    //here you can check on the item.name 
    //and if that matches whatever you need, you can do your magic
}

So, a function that will just add a minus sign to all parameters and return a URL string from that could look something like this:

func addMinusToParameters(inURLString urlString: String) -> String? {
    guard let components = NSURLComponents(string: urlString),
          let queryItems = components.queryItems else {
        return nil
    }

    var minusItems = [NSURLQueryItem]()
    for item in queryItems where item.value != nil {
        minusItems.append(NSURLQueryItem(name: item.name, value: "-\(item.value!)"))
    }
    components.queryItems = minusItems
    guard let minusUrl = components.URL else {
        return nil
    }
    return minusUrl.absoluteString
}

And an example:

if let minusString = addMinusToParameters(inURLString: "http://www.corpebl.com/blog.php?show=250&id=23") {
    print(minusString) //gives you "http://www.corpebl.com/blog.php?show=-250&id=-23"
}

And yes, it might seem like more work but I think it is more flexible as well.

Hope you can use this.

Upvotes: 2

Martin R
Martin R

Reputation: 539695

NSURLComponents allows you to access and modify each part of a URL selectively. Here an example how to replace show=<num> by show=<-num> even in a list of multiple query items:

var urlString = "http://www.corpebl.com/blog.php?foo&show=250&bar=baz"

if var urlComponents = NSURLComponents(string: urlString),
    var queryItems = urlComponents.queryItems {
    for (idx, query) in queryItems.enumerate() {
        if query.name == "show", let val = query.value, let num = Int(val) {
            queryItems[idx] = NSURLQueryItem(name: query.name, value: String(-num))
        }
    }
    urlComponents.queryItems = queryItems
    urlString = urlComponents.string!
}

print(urlString) // http://www.corpebl.com/blog.php?foo&show=-250&bar=baz

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

You should be able to do it by replacing = with =-:

let s = "http://www.corpebl.com/blog.php?show=250"
let res = s.stringByReplacingOccurrencesOfString("=", withString: "=-", options: NSStringCompareOptions.LiteralSearch, range: nil)

This is slightly dangerous in case you have multiple equal signs in the string. A more precise approach would be locating the first (or the last) equal sign, and composing the resulting string manually:

let s : NSString = "http://www.corpebl.com/blog.php?show=250"
let p = s.rangeOfString("=")
if p.location != NSNotFound {
    let res : String = s.stringByReplacingCharactersInRange(p, withString: "=-")
}

Upvotes: 4

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

you can use replacing = with =-, by using stringByReplacingOccurrencesOfString concept

 let aString: String = "http://www.corpebl.com/blog.php?show=250"
    let newString = aString.stringByReplacingOccurrencesOfString("=", withString: "=-")
    print(newString)

you get output like

enter image description here

Upvotes: 3

Related Questions