【发布时间】:2013-06-03 06:42:07
【问题描述】:
我将如何用数组中的值替换字符串中的字符或字符串的所有实例?
例如
String testString = "The ? ? was ? his ?";
String[] values = new String[]{"brown", "dog", "eating", "food"};
String needle = "?";
String result = replaceNeedlesWithValues(testString,needle,values);
//result = "The brown dog was eating his food";
方法签名
public String replaceNeedlesWithValues(String subject, String needle, String[] values){
//code
return result;
}
【问题讨论】:
-
这是家庭作业吗?
-
//code (..for you to attempt) -
不。不是作业。我可以尝试编写一些使用大量子字符串的东西,但我知道有一种更有效的方法可以做到这一点。