Java 字符串替换android?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18523262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:44:58  来源:igfitidea点击:

String replace android?

javaandroideclipsestringpdf

提问by Miguel

Im trying to replace a character : on a system date generated so I can put it as a title. It gives me the format: "ago 29, 2013 7:42:19 p.m.", so I need to change the ":" for " ", with an string replace. But I dont know how to do it. Ill appreciate your help. Heres my code:

我试图替换一个字符 : 在生成的系统日期上,所以我可以把它作为标题。它给了我格式:“ago 29, 2013 7:42:19 pm”,所以我需要将“:”更改为“”,并替换为字符串。但我不知道该怎么做。非常感谢您的帮助。这是我的代码:

    public void createPDF()
    {
        Document doc = new Document();


         try {
             Date date = new Date();
            String dateTime = DateFormat.getDateTimeInstance().format(date);
             File sdCard = Environment.getExternalStorageDirectory();
             File dir = new File (sdCard.getAbsolutePath() + "/Bitacora");
             dir.mkdirs();
             File file = new File(dir, "Bitácora "+idetotrocliente.getText().toString()+", "+dateTime+etsitio.getText().toString()+".pdf");
             FileOutputStream fOut = new FileOutputStream(file);
                 }
             catch(Exception)
             {

             }
    }

采纳答案by newuser

Try String.replace(old, new)

尝试String.replace(old, new)

String dateTime = "ago 29, 2013 7:42:19 p.m.";
dateTime = dateTime.replace(":", " ");
System.out.println("dateTime : "+dateTime);

output : dateTime : ago 29, 2013 7 42 19 p.m.

输出 : dateTime : ago 29, 2013 7 42 19 p.m.

回答by Juned Ahsan

Is it what you want:

是你想要的吗:

 String orig = "ago 29, 2013 7:42:19 p.m.";
String updated = orig.replace(":", " ");
System.out.println(updated);