Java 编写一个方法,将字符串中的所有空格替换为';%20';?
我有一个关于编程问题的问题,来自Gayl Laakmann McDowell的《破解代码访谈》,第五版 我不确定我的答案有什么问题?这与书中给出的答案大不相同Java 编写一个方法,将字符串中的所有空格替换为';%20';?,java,arrays,string,char,Java,Arrays,String,Char,我有一个关于编程问题的问题,来自Gayl Laakmann McDowell的《破解代码访谈》,第五版 我不确定我的答案有什么问题?这与书中给出的答案大不相同 public String replace(String str){ String[] words = str.split(" "); StringBuffer sentence = new StringBuffer(); for(String w: words){ sentence.append("%2
public String replace(String str){
String[] words = str.split(" ");
StringBuffer sentence = new StringBuffer();
for(String w: words){
sentence.append("%20");
sentence.append(w);
}
return sentence.toString();
}
在此循环中,程序在每个单词前添加
%20
:
这将产生不正确的结果,例如,对于ab
,它将给出%20a%20b
有一个更简单的解决方案:
public String replace(String str) {
return str.replaceAll(" ", "%20");
}
或者,如果您确实不想使用.replaceAll
,请这样写:
public String replace(String str) {
String[] words = str.split(" ");
StringBuilder sentence = new StringBuilder(words[0]);
for (int i = 1; i < words.length; ++i) {
sentence.append("%20");
sentence.append(words[i]);
}
return sentence.toString();
}
公共字符串替换(字符串str){
String[]words=str.split(“”);
StringBuilder语句=新的StringBuilder(单词[0]);
for(int i=1;i
您还可以执行以下操作,以替换任何空格
String s = "Hello this is a string!";
System.out.println(replaceSpace(s, "%20"));
public static String replaceSpace(String s, String replacement) {
String ret = s.replaceAll(" *", replacement);
return ret;
}
给予
书中的问题是: 注意:如果用Java实现,请使用字符数组,以便 您可以就地执行此操作 它还表示作为输入获取的字符数组足够长,可以容纳修改后的字符串。
通过使用
split
和StringBuffer
可以使用额外的O(n)空间。这就是为什么您的答案差异很大且不正确(除了添加额外的%20”
)的原因。最简单的方法之一:
public void replaceAll( String str )
{
String temp = str.trim();
char[] arr = temp.toCharArray();
StringBuffer sb = new StringBuffer();
for( int i = 0; i < arr.length; i++ )
{
if( arr[i] == ' ' )
{
sb.append( "%20" );
}
else
{
sb.append( arr[i] );
}
}
}
public void replaceAll(字符串str)
{
字符串温度=str.trim();
char[]arr=temp.toCharArray();
StringBuffer sb=新的StringBuffer();
对于(int i=0;i
我正在使用匹配
和replaceAll
它工作得很好
public class ReplaceSpaces {
public static void main(String[] args) {
String text = " Abcd olmp thv ";
if(text.matches(".*\\s+.*")){
System.out.println("Yes I see white space and I am replacing it");
String newText = text.replaceAll("\\s+", "%20");
System.out.println(newText);
}
else{
System.out.println("Nope I dont see white spaces");
}
}
}
输出
是,我看到空白,我正在替换它
%20Abcd%20olmp%20thv%20
不同,因为它给出了错误的结果?或者不同,因为这是一个正确的答案,但与书中的答案不同?这应该已经起作用了,请说明它是如何起作用的,replaceAll(“,“%20”)
将替换%20
中的所有单个空格,但如果您是为了对URL进行编码,请查看urlcoder句子=句子。替换(“,“%20”)代码>?@arynaq,因为它不是书中的答案(而且不正确),而且书中的答案要复杂得多:你的句子将以“%20”开头。对于第二个代码示例:你也可以用第一个单词初始化StringBuilder:StringBuilder句子=新的StringBuilder(单词[0])代码>。好主意,完成了,谢谢@Tom!顺便说一句,我想你可能会喜欢那里,我已经在那里有一个帐户,但回答那里需要更多的时间,我通常没有:(.答案应该总是带有一点解释,所以如果你可以补充,那就太好了!谢谢!
public static String replaceSpaceInString(String string,String toreplace){
String replacedString = "";
if(string.isEmpty()) return string;
string = string.trim();
if(string.indexOf(" ") == -1)return string;
else{
replacedString = string.replaceAll("\\s+",toreplace);
}
return replacedString;
}
private static String applyReplaceOperationWithCount(String str) {
if (StringUtils.isEmpty(str)) { //if string is null or empty, return it
return str;
}
char[] strChar = str.toCharArray();
int count = 0; //count spaces in the string to recalculate the array length
for (char c : strChar) {
if (c == ' ') {
count++;
}
}
if (count == 0) { // if there are no spaces in the string, return it
return str;
}
int length = strChar.length;
char[] newChar = new char[length + (count * 2)]; // 1 char will be replaced by 3 chars. So the new length should be count*2 larger than original
int index = 0;
for (char c : strChar) {
if (c != ' ') { // if char is not a space just push it in the next available location
newChar[index++] = c;
} else { // if char is a space just push %,2,0
newChar[index++] = '%';
newChar[index++] = '2';
newChar[index++] = '0';
}
}
return new String(newChar); // convert the new array into string
}
public class ReplaceSpaces {
public static void main(String[] args) {
String text = " Abcd olmp thv ";
if(text.matches(".*\\s+.*")){
System.out.println("Yes I see white space and I am replacing it");
String newText = text.replaceAll("\\s+", "%20");
System.out.println(newText);
}
else{
System.out.println("Nope I dont see white spaces");
}
}
}
public static String replaceSpaceInString(String string,String toreplace){
String replacedString = "";
if(string.isEmpty()) return string;
string = string.trim();
if(string.indexOf(" ") == -1)return string;
else{
replacedString = string.replaceAll("\\s+",toreplace);
}
return replacedString;
}