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
String replace android?
提问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
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);