I have a string in java:
String text = "A, B, C, D, ";
How I can to get "A, B, C and D"
?
I have "A, B, C, D" as well: text.substring(0, text.lastIndexOf(",")), but how do I get the result I want?
Thanks!!!
I have a string in java:
String text = "A, B, C, D, ";
How I can to get "A, B, C and D"
?
I have "A, B, C, D" as well: text.substring(0, text.lastIndexOf(",")), but how do I get the result I want?
Thanks!!!
How about something like
text = text.replaceAll(", (\\w+), $", " and $1");
replaceAll
uses regex as first parameter and can use parts matched by this regex in replacement via $x
where x
refers to part matched with group number x
(to put it shortly groups are parts or regex from parenthesis).
So this regex will match , oneOrMoreCharacters,
which is right before the end of string which in regex can be represented with $
.
DEMO:
System.out.println("A, B, C, D, ".replaceAll(", (\\w+), $", " and $1"));
output: A, B, C and D
EDIT
If your D
part can also contain non alphanumeric characters then instead of \\w
use [^,]
which represents all characters except ,
. So try with
text = text.replaceAll(", ([^,]+), $", " and $1");
You could do the following:
You can do this using StringBuilder:
String myStr = "A, B, C, D,";
StringBuilder builder = new StringBuilder(myStr);
builder.replace(builder.lastIndexOf(","), builder.lastIndexOf(",")+1, "");
builder.replace(builder.lastIndexOf(","), builder.lastIndexOf(",")+1, " and ");
String result = builder.toString();
System.out.println(result);//Should print A, B, C and D
I have not tested this code. But you get the idea.
String text = "A, B, C, D, ";
String[] split=text.split(",");
String ans="";
for(int i=0;i<split.length;i++){
if(i==split.length-2){
ans+= "and "+ split[i].replace(" ","");
break;
}
if(i>0){
ans+=", ";
}
ans+=split[i];
}
System.out.println(ans); // what you wanted
It seems like you are using code like
List<String> list = Arrays.asList("A", "B", "C", "D");
String text = "";
for (String token : list) {
text += token + ", ";
}
System.out.println(text);
to generate your text
value.
Instead, write the first value. Then write everything but the last value. Then write the last value with the and
prepended.
List<String> list = Arrays.asList("A", "B", "C", "D");
String text = "";
if (!list.isEmpty()) {
text += list.get(0);
for (int i = 1; i < list.size() - 1; i++) {
text += ", " + list.get(i);
}
if (list.size() > 1)
text += " and " + list.get(list.size() - 1);
}
System.out.println(text);
Obviously, use a StringBuilder
if appropriate. The above prints
A, B, C and D
The beauty of this is that it works with any number of values, doing the least possible number of checks for lengths and tokens remaining. With a single element, it would print only that value
A
With two elements (A and B), it would print
A and B
And so on.
You could try something like the following:
String text = "A, B, C, D, ";
String[] a=text.split(",");
int i=a.length-1;
while(i>=0 && (a[i]==null || a[i].trim().isEmpty()))
i--;
System.out.println(a[i]);