Java StringBuffer to String with Comma Separation
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:
toString(): ThetoString()method converts the StringBuffer object to a String.split(','): Thesplit()method splits the String based on the comma delimiter, creating a String array.String.join(',', strArray): Thejoin()method concatenates the elements of the String array with commas in between, producing the final comma-separated string.
原文地址: https://www.cveoy.top/t/topic/kTaU 著作权归作者所有。请勿转载和采集!