1

I am taking a description of the weather using rainfall in millimeters and converting it to inches. I have worked out to calculate the rainfall in inches and replace it in the string. Because the description also has other numbers, I want to only replace the first instance of the number I converted (rainfall is listed first).

weatherConverted = weatherConverted.stringByReplacingOccurrencesOfString("\(precipitationInMillimeters)", withString: "\(precipitationInInches)")

precipitationInMillimeters is an Integer, hence the escape sequence. I know that the function rangeOfString(_:) will return the range of the first instance of the string I put in it, but I don't know the proper syntax for calling that function and getting it to return to the range parameter.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
btechdog
  • 15
  • 6
  • Show what the original string looks like and what you would like the desired output to be. – Daniel Storm Jun 08 '15 at 21:32
  • Original String :"Heavy rain (total 35mm), heaviest during Tue morning. Warm (max 26°C on Wed afternoon, min 19°C on Tue morning). Wind will be generally light." Desired String: "Heavy rain (total 1.38in), heaviest during Tue morning. Warm (max 79°F on Wed afternoon, min 66°F on Tue morning). Wind will be generally light." – btechdog Jun 08 '15 at 21:37
  • The desired string has already been achieved with the rainfall amount. The purpose of only finding the first instance of the number 35 (in this case) is to protect the result against the hypothetical scenario that the rainfall in mm is equal to the temperature in celcius. – btechdog Jun 08 '15 at 21:42
  • There is only one "35mm" in that string so what's the problem? Replace "35mm" with "38in" and you're done. – matt Jun 08 '15 at 21:42
  • This is a simple weather checking app where it is possible that the mm of rainfall equals the temperature when the data is loaded. The string is pulled from weather-checker.com – btechdog Jun 08 '15 at 21:44
  • Find 35mm instead of just 35. Then trim the mm from 35 and convert 35 to inches. – Daniel Storm Jun 08 '15 at 21:46
  • 1
    Wow! That was an easy solution. Thank you!!! It actually shortened my code a line. – btechdog Jun 08 '15 at 21:49
  • Of course, I would still like learn how to call the `rangeOfString(_:)` function from within the `stringByReplacingOccurancesOfString()` method's `range:` parameter. Any pointers would be helpful. Thanks. – btechdog Jun 08 '15 at 21:56

0 Answers0