java正则表达式去掉小数点后面多余的0
出处:java正则去掉小数点后多余0 - - ITeye博客 http://jiauwu.iteye.com/blog/1240794
/**
* 去掉多余的.与0
* @author Hust
* @Time 2011-11-7
*/
public class TestString {
public static void main(String[] args) {
Float f = 1f;
System.out.println(f.toString());//1.0
System.out.println(subZeroAndDot("1"));; // 转换后为1
System.out.println(subZeroAndDot("10"));; // 转换后为10
System.out.println(subZeroAndDot("1.0"));; // 转换后为1
System.out.println(subZeroAndDot("1.010"));; // 转换后为1.01
System.out.println(subZeroAndDot("1.01"));; // 转换后为1.01
}
/**
* 使用java正则表达式去掉多余的.与0
* @param s
* @return
*/
public static String subZeroAndDot(String s){
if(s.indexOf(".") > 0){
s = s.replaceAll("0+?$", "");//去掉多余的0
s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
}
return s;
}
}
按指定符号拆分
String string = "125,123,323";
if (!TextUtils.isEmpty(string )) {
String[] pathStrings = item.getPath().split(",");
if (pathStrings != null && pathStrings.length >= 1) {
imgPath = pathStrings[0];
}
}
想使用“.”进行拆分,记得“\.”,特殊符号要使用转义符。
比如想以\n来拆分:
List<String> stringList = new ArrayList<>();
if (!StringUtils.isEmpty(content)) {//content为要进行拆分的字符串
String[] splits = content.split("\\n");
for (int i = 0; i < splits.length; i++) {
if (!StringUtils.isEmpty(splits[i])) {
stringList.add(splits[i]);
}
}
}
查找某字符串在字符串中所有所在位序
//比如:str="你好这是在测试你好这是在测试" key="你好" 要在str中找出所有key所在的位序,存在一个List<Integer>中
private List<Integer> getSearchAllIndex(String key, String str) {
List<Integer> list = new ArrayList<>();
if (StringUtils.isEmpty(key)) {
return list;
}
int index = str.indexOf(key);//第一个出现的索引位置
while (index != -1) {
list.add(index);
index = str.indexOf(key, index + 1);//从这个索引往后开始第一个出现的位置
}
return list;
}
修改字符串中指定位序的字符串颜色
效果:data = “你好这是在测试你好这是在测试” mSearchContent = “你好”
把data中的所有"你好”的显示颜色改为指定颜色color_search_content
SpannableStringBuilder builder = new SpannableStringBuilder(data);
List<Integer> includeIndexList = getSearchAllIndex(mSearchContent, data);
if (includeIndexList.size() > 0) {
for (int i = 0; i < includeIndexList.size(); i++) {
ForegroundColorSpan redSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.color_search_content));
builder.setSpan(redSpan, includeIndexList.get(i), includeIndexList.get(i) + mSearchContent.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}else {
ForegroundColorSpan span = new ForegroundColorSpan(mContext.getResources().getColor(R.color.color_555555));
builder.setSpan(span, 0, data.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
tvTitle.setText(builder);
<color name="color_search_content">#488EEE</color>
<color name="color_555555">#555555</color>
字符串转换成List
String内部以逗号分隔数据,需把数据转换成List.
String str= "a,b,c";
if (!StringUtil.isEmpty(str)) {
List<String> list= Arrays.asList(str.split(","));
}
string使用占位符
int型:
<string name="data_number">%d条</string>
mTextView.setText(getString(R.string.data_number, 7));
String型:
<string name="my_info">我的信息:%s</string>
mTextView.setText(String.format(getString(R.string.my_info),"未知"));