This code demonstrates how to convert a Java StringBuffer to a String and separate its content with commas.

StringBuffer batchReplaceStr = new StringBuffer();
// Assume batchReplaceStr contains the desired string

// Convert StringBuffer to String
String str = batchReplaceStr.toString();

// Split the string into an array using comma as delimiter
String[] strArray = str.split(',');

// Join the array elements with commas
String result = String.join(',', strArray);

System.out.println(result);

This code will print the result as a comma-separated string.

Explanation:

  1. toString(): The toString() method converts the StringBuffer object to a String.
  2. split(','): The split() method splits the String based on the comma delimiter, creating a String array.
  3. String.join(',', strArray): The join() method concatenates the elements of the String array with commas in between, producing the final comma-separated string.
Java StringBuffer to String with Comma Separation

原文地址: https://www.cveoy.top/t/topic/kTaU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录