Sunder
Sunder

Reputation: 503

stringByReplacingOccurrencesOfString is not working?

I am trying to translate this Objective-C code :

if ([[self.appStoreCountry stringByReplacingOccurrencesOfString:@"[A-Za-z]{2}" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, 2)] length])

So I tried to wrote this code :

if !self.appStoreCountry.stringByReplacingOccurrencesOfString("[A-Za-z]{2}", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range: Range(start: 0, end: 2)).isEmpty

The problem is that the stringByReplacingOccurrencesOfString part does not seem to return a string. I have the error message from xcode : Cannot convert the expression's type 'Bool' to type 'String'.

I investigated a little more and tried :

let replaced = self.appStoreCountry.stringByReplacingOccurrencesOfString("[A-Za-z]{2}", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range: Range(start: 0, end: 2))

The error message is similar but even more weird : Cannot convert the expression's type 'String' to type 'String'.

Am I doing something wrong or is there a bug I should submit to Apple?

Upvotes: 2

Views: 12305

Answers (5)

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

Keep in mind that Foundation's types are different from Swift's types. A String in Swift is not an NSString, and a Range is not an NSRange. This does work:

let country = self.appStoreCountry // if self.appStoreCountry is an NSString
let country: NSString = self.appStoreCountry // if self.appStoreCountry is a Swift string

let replaced = country.stringByReplacingOccurrencesOfString("[A-Za-z]{2}", withString: "", options: .RegularExpressionSearch, range: NSMakeRange(0, 2))

Also note the short-hand notation for .RegularExpressionSearch, which makes using enums in Swift a bit easier.

Upvotes: 7

Leo Dabus
Leo Dabus

Reputation: 236528

extension String{
    func exclude(find:String) -> String {
        return stringByReplacingOccurrencesOfString(find, withString: "", options: .LiteralSearch, range: nil)
    }
    func replaceAll(find:String, with:String) -> String {
        return stringByReplacingOccurrencesOfString(find, withString: with, options: .LiteralSearch, range: nil)
    }
}
"1-2-3-4-5-6-7-8-9-0".exclude("-")                 // "1234567890" 
"1-2-3-4-5-6-7-8-9-0".replaceAll("-", with: "+")   // "1+2+3+4+5+6+7+8+9+0"

Upvotes: 4

Chéyo
Chéyo

Reputation: 9367

let maskCharSet = NSCharacterSet(charactersInString: " ()-.")
let cleanedString = phoneNumber.componentsSeparatedByCharactersInSet(maskCharSet).reduce("", combine: +)

Upvotes: 0

PREMKUMAR
PREMKUMAR

Reputation: 8349

Try This code:

var originalString="Hello World"

    var needToReplaceString = "Hello"
    var replaceString = " Hi"


    var newString = originalString.stringByReplacingOccurrencesOfString(needToReplaceString, withString: replaceString, options: nil, range: nil)

    println(newString)

Upvotes: 1

LombaX
LombaX

Reputation: 17374

Open a Playground and add this code, now it's correct:

var str = NSString(CString: "en")

str = str.stringByReplacingOccurrencesOfString("[A-Za-z]{2}", withString: "", options: NSStringCompareOptions.RegularExpressionSearch, range: NSRange(location: 0, length: 2))

if str == ""
{
    var a = "true"
} else {
    var b = "false"
}

Your problems were:

  • .isEmpty is not a method of NSString
  • You were using Range() and not NSRange as required by the method .stringByReplacingOccurrencesOfString of NSString
  • since your replace returns an empty string and not a nil object, you have to test for "" in the if statement

Upvotes: 0

Related Questions