// Build a matcher to find numbers followed by "C" within the variable "Metric" // The following regex is: 「(\d+(?:\.\d*)?)C\b」 Matcher m = Pattern.compile("(\\d+(?:\\.\\d*)?)C\\b").matcher(metric); StringBuffer result = new StringBuffer(); // We'll build the updated copy here while (m.find()) { float celsius = Float.parseFloat(m.group(1)); // Get the number, as a number int fahrenheit = (int) (celsius * 9/5 + 32); // Convert to a Fahrenheit value m.appendReplacement(result, fahrenheit + "F"); // Insert it } m.appendTail(result); System.out.println(result.toString()); // Display the result ----------------------------------------------------------------------------- Copyright 1997-2024 Jeffrey Friedl