【问题标题】:String.repalceAll() not working?String.replaceAll() 不工作?
【发布时间】:2015-08-12 11:27:54
【问题描述】:
String points;
        String points2;
        @Override
        protected Void doInBackground(Integer... params) {

            try {
                cursor = dbEditor.getPoints(params[0]);
                cursor.moveToFirst();
                strPoints = cursor.getString(cursor.getColumnIndex("points"));
                points = "points:"+strPoints;
                points = points.replace(')','}');
                points = points.replace('(','{');
                points2 = points.replace("lat\"/lng: ","");

结果还是:

点数:["lat/lng: {43.8499843,26.5009921}","lat/lng: {43.8499843,26.5009921}","lat/lng: {43.8499843,26.5009921}","lat/lng: {43.8499843,26.5009921}","lat/lng: {43.8499843,26.5009921}","lat/lng: {43.8499843,26.5009921}","lat/lng: {43.8499843,26.5009921}","lat/lng: {43.8499843,26.5009921}","lat/lng: {43.8499329,26.5009746}","lat/lng: {43.8499114,26.5009419}","lat/lng: {43.8499114,26.5009419}","lat/lng: {43.8499114,26.5009419}"]

有什么建议为什么replaceAll() 不起作用?

【问题讨论】:

  • 你在哪里不工作replaceAll()
  • 你没有使用replaceAll,你使用的是replace
  • 为什么要转义最后一行的“/”字符?
  • 无论如何... replaceAll() 也不能正常工作...没有用“”空字符串替换该子字符串..
  • 尽管您没有使用 replaceAll();为什么要替换,没有lat"/lng:

标签: java


【解决方案1】:

我看到您的)( 被替换为}{

"lat/lng: {43.8499843,26.5009921}"

没有被替换的是lat/long。因为您要替换字符串没有的lat"/lng:

字符串有lat//lang

所以你的最后一次替换必须是。

points = points.replace("lat//lng: ","");

【讨论】:

    【解决方案2】:

    你的最后一次替换应该是这样的:-

    points2 = points.replaceAll("lat\\/lng: ","");
    

    请看下面的代码:-

    public static void main(String[] args){
        String str="lat/lng: {43.8499843,26.5009921}\",\"lat/lng: {43.8499843,26.5009921}\",\"lat/lng: ";
        //Problem is the first expression of replaceAll is a regex so, / has a special meaning
        //in regex, you need to escape it
        System.out.println(str.replaceAll("lat\\/lng:", ""));
    }
    
    Output: {43.8499843,26.5009921}"," {43.8499843,26.5009921}"," 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-09
      • 2013-05-30
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多