title: Replace numeric of different length in a string with different text length
body: <p><strong>Problem</strong>: Numeric is of different length could be 1, 200, 1000, 39 99995 etc. And need to replace with a text eg. "Apple" which is of different lenth comapring to the numeric values.</p>
  
  <p>let str: String = "hello i have 1313 object of 10 string class with 1 object, similar to 9999 errors"</p>
  
  <p>Expected Result = "hello i have <strong>Apple</strong> object of <strong>Apple</strong> string class with <strong>Apple</strong> object, similar to <strong>Apple</strong> errors"</p>
  
  <p>I have tried with below code:</p>
  
  <pre><code> var originalString: String = "hello i have 1313 object of 10 string class with 1 object, similar to 9999 errors"
  
      let strippedString: NSMutableString = NSMutableString(capacity: originalString.characters.count)
      var numArray: [String] = []
      var locArray: [NSNumber] = []
      var scanner: NSScanner = NSScanner(string:originalString)
      let numbers: NSCharacterSet = NSCharacterSet(charactersInString: "0123456789")
      while scanner.atEnd == false {
          var buffer: NSString?
          if scanner.scanCharactersFromSet(numbers, intoString: &amp;buffer) {
              strippedString.appendString(buffer! as String)
              numArray.append(buffer! as String)
              locArray.append(scanner.scanLocation)
          }
          else {
  
              scanner.scanLocation = (scanner.scanLocation + 1)
          }
      }
      for (index, _) in numArray.enumerate() {
          var loc : Int = Int(locArray[index] ) - (String(numArray[index]).characters.count)
          let len = String(numArray[index]).characters.count
          let dupStr = "Apple"
          if(index != 0 &amp;&amp; len !=  dupStr.characters.count)
          {
              loc = loc + (dupStr.characters.count - len) + 1
          }
          originalString.replaceRange(originalString.startIndex.advancedBy(loc)..&lt;originalString.startIndex.advancedBy(loc + len), with: dupStr)
      }
      print(originalString)
  </code></pre>