我的代碼如下。在將 DD 附加到字串后,我需要用單引號為字串中的每個單詞添加單引號。
public class Main
{
public static void main(String[] args) {
String ChkboxIds = "'a1b2c3','321cba','123abc'";
String checkBoxId = null;
String checkId = null;
StringBuilder checkIDS = new StringBuilder("'");
for(int i=0;i<=ChkboxIds.split(ChkboxIds, ',').length;i ){
checkBoxId = "DD" ChkboxIds.split(",")[i].replace("'","") "," checkBoxId;
checkId = checkBoxId.substring(0, checkBoxId.length() - 5);
System.out.println("---PRINT---" checkId);
for(int j=0;j<i;j ){
checkIDS..append('\'').append(checkId.split(",")).append('\'').append(',');
System.out.println("---PRINT123----" checkIDS);
}
}
}
}
我也嘗試過使用 StringBuffer。請在此處指出您的答案。我得到的輸出是一些垃圾資料,而我需要在開始時附加 dd 的單詞。
預期輸出:'DDa1b2c3','DD321cba','DD123abc'
uj5u.com熱心網友回復:
問題
在
.append(checkId.split(","))您附加一個的位置的問題,String[]所以它的表示是它的哈希碼不需要第二個回圈,每個單詞需要一個回圈,不需要內回圈
你的分割是錯誤的,你需要
ChkboxIds.split(","),你不需要與分隔符相同的字串
使固定
你可以做的比這簡單得多
- 逗號分割
- 洗掉引號,追加
DD,添加引號 - 保存在陣列中的同一位置
- 用逗號連接陣列
String chkboxIds = "'a1b2c3','321cba','123abc'";
String[] splitted = chkboxIds.split(",");
String checkBoxId;
for (int i = 0; i < splitted.length; i ) {
checkBoxId = "DD" splitted[i].replace("'", "");
splitted[i] = "'" checkBoxId "'";
}
String result = String.join(",", splitted);
System.out.println(result);
// 'DDa1b2c3','DD321cba','DD123abc'
正則運算式的力量
String chkboxIds = "'a1b2c3','321cba','123abc'";
String result = chkboxIds.replaceAll("'(\\w )'", "'DD$1'");
uj5u.com熱心網友回復:
試試這個:
var ids = Arrays.stream( ChkboxIds.split( "," ) ) // Separate the words
.map( s -> s.replace( '\'', '' ) ) // Strip the single quotes
.map( s -> "DD%s".formatted( s ) ) // Prepend with "DD"
.collect( Collectors.joining( "','", "'", "'" ) );
System.out.println( ids );
或者:
var ids = Arrays.stream( ChkboxIds.split( "," ) ) // Separate the words
.map( s -> s.replaceFirst( "'", "'DD" ) ) // Replace the first single quote with the prefix
.collect( Collectors.joining( "," ) );
System.out.println( ids );
有關詳細資訊,請參閱相應的 Javadoc。
uj5u.com熱心網友回復:
對于更簡單的解決方案,您可以嘗試下面的代碼。
public class Main
{
public static void main(String[] args) {
String ChkboxIds = "'a1b2c3','321cba','123abc'";
//Replace all single quotes from the ChkboxIds, split them using comma delimiter, and store it to variable array
String[] ChkboxIdsNew = ChkboxIds.replace("'","").split(",");
String checkIDS = ""; //String variable to be used to store the check ids
//Loop through the ChkboxIdsNew array
for (int i = 0; i < ChkboxIdsNew.length; i ) {
//Only append comma if i is greater than zero
if (i > 0) {
checkIDS = checkIDS ",";
}
//Concatenate the single quotes and prefix DD then append it to checkIDS
checkIDS = checkIDS "'DD" ChkboxIdsNew[i] "'";
}
System.out.println(checkIDS);
}
}
輸出:

uj5u.com熱心網友回復:
使用正則運算式:如果單個單詞不包含額外的單引號:
String ChkboxIds = "'a1b2c3','321cba','123abc'";
ChkboxIds = ChkboxIds.replaceAll(",'",",'DD").replaceAll("^'","'DD");
使用迭代:
String arChkBoxIds[]= ChkboxIds.split(",");
StringBuilder checkIDS1 = new StringBuilder("");
for (String chkBoxId:arChkBoxIds){
checkIDS1.append("'DD").append(chkBoxId.substring(1)).append(",");
}
checkIDS1.setLength(checkIDS1.length()-1);
System.out.println(checkIDS1);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/399297.html
下一篇:從python中的字串中洗掉單詞
