This article describes the method of removing the last redundant comma when java splicing strings. Share it for your reference. The specific analysis is as follows:
Let's look at the following code first:
for (int t = 0; t < memberLen; t++) { memTemp = stafferMap.get(strMember[t]); if(memTemp != null){ memberNames += memTemp + ","; }}
In the above code, the string spliced will have an extra "," such as: "str1,str2,str3,". To remove the comma after str3, you can use the following methods:
memberNames = memberNames.substring(0, memberNames.length()-1);
For example, when Team1=test ', 'U1-Team ', 'V-Team ', '
How to change to:
Team1= 'test ', 'U1-Team ', 'V-Team '
Available:
Team1 = " ' " + Team1.Substring(0, Team1.Length - 2);
The following are the additions from other netizens:
If you splice, you can use the following code
StringBuilder sdb = new StringBuilder();for ( int t = 0; t < memberLen; t++ ){memTemp = stafferMap.get( strMember[t] );if ( memTemp != null ){if ( sbd.length > 0 ) {sbd.append( "," ).append( memTemp );}else{sbd.append( memTemp );}}}System.out.pretln( "No extra comma:" + sbd.toString() );
I hope this article will be helpful to everyone's Java programming.