整理:String字符串的相关处理

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),"未知"));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值