How do I only replace the first instance of a number in a string?

80 views Asked by At

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.

0

There are 0 answers