To remove the ", " part which is immediately followed by end of string, you can do:
str = str.replaceAll(", $", "");
This handles the empty list (empty string) gracefully, as opposed to lastIndexOf / substring solutions which requires special treatment of such case.
Example code:
String str = "kushalhs, mayurvm, narendrabz, ";
str = str.replaceAll(", $", "");
System.out.println(str); // prints "kushalhs, mayurvm, narendrabz"
NOTE: Since there has been some comments and suggested edits about the ", $" part: The expression should match the trailing part that you want to remove.
- If your input looks like
"a,b,c,", use",$". - If your input looks like
"a, b, c, ", use", $". - If your input looks like
"a , b , c , ", use" , $".
I think you get the point.
Answer from aioobe on Stack OverflowTo remove the ", " part which is immediately followed by end of string, you can do:
str = str.replaceAll(", $", "");
This handles the empty list (empty string) gracefully, as opposed to lastIndexOf / substring solutions which requires special treatment of such case.
Example code:
String str = "kushalhs, mayurvm, narendrabz, ";
str = str.replaceAll(", $", "");
System.out.println(str); // prints "kushalhs, mayurvm, narendrabz"
NOTE: Since there has been some comments and suggested edits about the ", $" part: The expression should match the trailing part that you want to remove.
- If your input looks like
"a,b,c,", use",$". - If your input looks like
"a, b, c, ", use", $". - If your input looks like
"a , b , c , ", use" , $".
I think you get the point.
You can use this:
String abc = "kushalhs , mayurvm , narendrabz ,";
String a = abc.substring(0, abc.lastIndexOf(","));
One may use string utility methods such as StringUtil.join to concatenate elements in an array or a collection object. Consult the StringUtil API's StringUtil.join entry.
For example:
StringUtils.join(["a", "b", "c"], "--") // => "a--b--c"
for ( String p : paramList )
{
if (result.length() > 0) result.append( ", " );
result.append( p );
}
I often use this idiom for this problem -- make a variable comma special ("") for the first time, and then restore it to the normal state (", ") for the second time and onward:
public void print() {
for(int i = 0; i < M.length; i++){
String comma = ""; // first time special
for(int j = 0; j < M[i].length; j++){
System.out.print(comma + M[i][j]);
comma = ","; // second time onward
}
System.out.println();
}
}
You should use an if else statement in your second for loop.
And instead of putting the comma at the back, it's better to put it at the front.
The sample code looks like this:
public void print(){
int[][] M = {{1,3},{2,3,4},{5,6},{5,2}};
for(int i = 0; i < M.length; i++){
for(int j = 0; j < M[i].length; j++){
if (j==0)
System.out.print(M[i][j]);
else
System.out.print(","+M[i][j]);
}
System.out.println();
}
}
The code will output
1,3
2,3,4
5,6
5,2
Many ways.
Use a
StringBuilderto make your string, then after the loop, if it is not empty, lop off the last character (sb.setLength(sb.length() - 1)).Use a boolean to track if this is the first time through the loop. If yes, just print the number. If not, print a comma, then the number. Set the boolean to false after.
Use string joining:
List<String> items = List.of("Hello", "World!");
System.out.println(String.join(", ", items));
Here's what I would do... use a StringBuilder and append to it inside of the loop like this.
Once the loop is finished, your output string will be ready and you can just remove the last character (which will be the comma)
StringBuilder sb = new StringBuilder();
for () {
sb.append(i+",");
}
// remove last comma
sb.setLength(sb.length() - 1);
System.out.println(sb.toString);
String start = "A,B,C,";
String result = start.subString(0, start.lastIndexOf(',', start.lastIndexOf(',') - 1));
Here is a fairly "robust" reg-exp solution:
Pattern p = Pattern.compile("((\\w,?)+),\\w+,?");
for (String test : new String[] {"A,B,C", "A,B", "A,B,C,",
"ABC,DEF,GHI,JKL"}) {
Matcher m = p.matcher(test);
if (m.matches())
System.out.println(m.group(1));
}
Output:
A,B
A
A,B
ABC,DEF,GHI
Acrually it is better to use JSON library to construct the object. In this case you can do it like this:
if (itr_1.hasNext()) {
jsonUIResponse.append("},");
} else {
jsonUIResponse.append("}");
}
My solution is adding a variable count. Add this declaration outside of the while loop
int count = 0;
Replace this statemet jsonUIResponse.append("{"); with the folowing statements
if(count == 0){
//for the first element, you only need to add {
jsonUIResponse.append("{");
}
else{
jsonUIResponse.append(",{");
}
then replace jsonUIResponse.append("},"); with these
jsonUIResponse.append("}");
count = 1;
You could remove the last character if every line as the comma:
resultString = resultString.substring(0, resultString.length()-1)
If you simply want to remove trailing comma from a String having comma separated values you can try replaceAll:
String str = "a,b,c,d,e,f,";
str = str.replaceAll(",$", "");
System.out.println(str);
It prints:
a,b,c,d,e,f
This solution also handles the empty list (empty string) gracefully, as opposed to lastIndexOf / substring solutions which requires special treatment of such case. In the above solution $ is a special symbol for matching the end of the string.
If you have an array of String then use it in a loop:
String[] stringsArray = new String[] {"a,b", "c,d", "e,f"};
for(String str : stringsArray) {
str = str.replaceAll(",$", "");
System.out.println(str);
}
And output is:
a,b
c,d
e,f