替换为空字符串替换原始字符串中所有字符周围的newChar [英] Replace with empty string replaces newChar around all the characters in original string

查看:118
本文介绍了替换为空字符串替换原始字符串中所有字符周围的newChar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个我正在使用Java String.replace方法的java代码。因此,在一种情况下测试替换方法时,我计划将垃圾值放在 String.replace(,);

I was just working on one of my java code in which I am using Java String.replace method. So while testing the replace method as in one situation I am planning to put junk value of String.replace("","");

等等测试我遇到了用其他值替换空值的条件,即 String.replace(,p) p原始字符串的所有字符周围的所有字符

so on Testing I came to a condition of replacing blank value with some other value i.e String.replace("","p") which replaced "p" everywhere around all the characters of the original String

示例:

String strSample = "val";
strSample = strSample.replace("","p");
System.out.println(strSample);

输出:

pvpaplp

任何人都可以解释为什么会这样吗?

Can anyone please explain why it works like this?

推荐答案

replace 查找您拥有以替换字符串开头的String的每个地方。例如如果你在香蕉中替换a,它会发现a3次。

replace looks for each place that you have a String which starts with the replaced string. e.g. if you replace "a" in "banana" it finds "a" 3 times.

但是,对于空字符串,它会在任何地方找到它,包括在最后一个字母之前和之后。

However, for empty string it finds it everywhere including before and after the last letter.

这篇关于替换为空字符串替换原始字符串中所有字符周围的newChar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆