how can i eleminate the last ,|
in this String
:
String model ="07:40,09:00,10:20,11:40,|09:00,10:20,11:40,|07:40,09:00,10:20,11:40,|10:20,11:40,|";
model is variable String
, and the result should like that :
String result="07:40,09:00,10:20,11:40,|09:00,10:20,11:40,|07:40,09:00,10:20,11:40,|10:20,11:40";
If it's always ,|
and you don't know if it will be present use replaceFirst
:
model = model.replaceFirst(",\\|$", "");
PS $
stands for end of String
String model ="07:40,09:00,10:20,11:40,|09:00,10:20,11:40,|07:40,09:00,10:20,11:40,|10:20,11:40,|";
String result = model.substring(0, model.length() -2);
System.out.println(result);
javadoc : substring(int beginIndex, int endIndex) : Returns a new string that is a substring of this string.
If this always shows up you can cut the end of the string. To do that you can do use this code:
result = model.substring(0, path.length() - 2);
If it only happens sometimes, you can do this:
if (model.substring(path.length - 2, path.length).equals(",|")) {
result = model.substring(0, path.length() - 2);
} else {
result = model
}
Try StringUtils :
import org.apache.commons.lang3.StringUtils;
// model = "07:40,09:00,10:20,11:40,|09:00,10:20,11:40,|07:40,09:00,10:20,11:40,|10:20,11:40,|"
model = StringUtils.removeEnd(model, ",|");
// Now
// model = "07:40,09:00,10:20,11:40,|09:00,10:20,11:40,|07:40,09:00,10:20,11:40,|10:20,11:40";
String correctedModel=new StringBuilder(model).deleteCharAt(model.lastIndexOf('|')).toString();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.