又一简单问题, 字符替换…… String类有一个函数replaceAll("","") 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 很遗憾,在我的JDK库里没有String.replaceAll("", "");唯一有的还只是Stringreplace('', '');所以只好自己写一个,好在不是很麻烦。public static String replace(String source, String oldStr, String newStr) { String result = ""; int k = 0; for (int i = 0; i < source.length(); ) { k = source.indexOf(oldStr, i); if (k > -1) { result += source.substring(i, k); result += newStr; i = k + oldStr.length(); } else { result += source.substring(i); break; } } return result;} To zhishao(zhi) : 你的方法又如何使用呢? 如果我需要将字符串中的"(双引号)替换为\"(反斜线+双引号), 则调用方法时的后两个参数应该如何写, 是否可以这样replace(str, "\"", "\\\"")是吗? 可是不行! /** * 使用rep替换s中的find。 * StringTokenizer的实现。 * @param s 源字符串 * @param find 匹配子串 * @param rep 替换字符串 * @return 处理后的源字符串 */ public final static String replace( String s, String find, String rep ) throws Exception { RE re = new RE( find ); return re.subst( s, rep ); } /** * 使用rep替换s中的find。 * StringTokenizer的实现。 * @param s 源字符串 * @param find 匹配子串 * @param rep 替换字符串 * @return 处理后的源字符串 */ public final static String replaceString( String s, String find, String rep ) { String out; int targetLength = s.length(); int replaceLength = rep.length(); int matchLength = find.length(); StringTokenizer st = new StringTokenizer( s, find ); int firstIndex = s.indexOf( find ); if ( firstIndex < 0 ) { // 无匹配 out = s; return out; } if ( firstIndex > 0 ) { out = ""; } else { // 以要替换的字符串开头 out = rep; } while ( st.hasMoreTokens() ) { out += st.nextToken() + rep; } out = out.substring( 0, out.length() - replaceLength ); int lastIndex = s.lastIndexOf( find ); // 以要替换的字符串结尾 if ( lastIndex == ( targetLength - matchLength ) ) { out = out + rep; } return out; } /** * @author beh * * @ Modified by : Greg Karbett * * Replaces one substring with another within a main string. * * @param s Main string. * @param find The substring to find. * @param rep Replaces the find substring. * @return The new string. */ public static String replaceStringBuffer( String s, String find, String rep ) { return replace( new StringBuffer( s ), find, rep ).toString(); } /** * @author beh * * @ Modified by : Greg Karbett * * Replaces one substring with another within a main string. * * @param sb Main string. (StringBuffer) * @param find The substring to find. * @param rep Replaces the find substring. * @return The new string. (StringBuffer) */ private static StringBuffer replace( StringBuffer sb, String find, String rep ) { StringBuffer buf = new StringBuffer( sb.toString() ); int startIndex = 0; int stringLocation = 0; boolean done = false; // Halts if they is not need to do any replacements if ( ! find.equals( rep ) ) { String s = buf.toString(); // Continues while more substring(s) (find) exist while ( ! done ) { // Grab the position of the substring (find) if ( ( stringLocation = s.indexOf( find, startIndex ) ) >= 0 ) { // Replace "find" with "rep" buf.delete( stringLocation, stringLocation + find.length() ); buf.insert( stringLocation, rep ); startIndex = stringLocation + rep.length(); s = buf.toString(); } else { done = true; } } } return buf; } 程序没问题啊,我测试过了。taozabc(Pluto)说的方法可行啊,没问题。字符串"\\\""不就是等于实际上的\"吗?我也测试通过了。 可以吗?我咋还是不行?我用以下:public class ReplaceChar{ public static void main(String[] args) { System.out.println(replace(args[0], "\"", "\\\"")); }}使用命令: java ReplaceChar tttase"sdfae得到的结果是:tttasesdfae把"丢掉了。 使用命令的问题java ReplaceChar tttase"sdfae这样运行的话,args[0]本身就等于tttasesdfae若要args[0]为tttase"sdfae,则需要用命令java ReplaceChar tttase\"sdfae faint运行命令java ReplaceChar tttase"sdfae此时args[0]本身就是tttasesdfae,如何能转换成功?要想args[0]是tttase"sdfae,运行命令必须是java ReplaceChar tttase\"sdfae才行另外:String的replaceAll方法是jdk1.4才带有的 to alphazhao(绿色咖啡) :小弟愚昧,为何“运行命令java ReplaceChar tttase"sdfae此时args[0]本身就是tttasesdfae”??谢谢1!!! 用字符分析器StringTokenizer分解,在合成不就完了麻 求各位大神帮忙解决下! 正则表达式如何表示要匹配无顺序字符呢? 初学者求助,为什么一直提示发现char,需求boolean,该怎么做,谢谢! 某公司一道笔试题,求思路及解答 关于移位 再请教个关于list 显示的问题,能实现这样的效果么? 急救!! jdbc的简单问题 APPLET里的数据库中的中文内容在LIST里是乱码 java1.3不支持中文输出,,?? java 的环境配置,请高手指教! 有谁知道,jbuilder7.0是调用了那些命令打包成EXE文件的?
String.replaceAll("", "");
唯一有的还只是
Stringreplace('', '');所以只好自己写一个,好在不是很麻烦。public static String replace(String source, String oldStr, String newStr) {
String result = "";
int k = 0;
for (int i = 0; i < source.length(); ) {
k = source.indexOf(oldStr, i);
if (k > -1) {
result += source.substring(i, k);
result += newStr;
i = k + oldStr.length();
}
else {
result += source.substring(i);
break;
}
} return result;
}
是否可以这样replace(str, "\"", "\\\"")
是吗? 可是不行!
* 使用rep替换s中的find。
* StringTokenizer的实现。
* @param s 源字符串
* @param find 匹配子串
* @param rep 替换字符串
* @return 处理后的源字符串
*/
public final static String replace( String s, String find, String rep )
throws Exception
{
RE re = new RE( find ); return re.subst( s, rep );
} /**
* 使用rep替换s中的find。
* StringTokenizer的实现。
* @param s 源字符串
* @param find 匹配子串
* @param rep 替换字符串
* @return 处理后的源字符串
*/
public final static String replaceString( String s, String find,
String rep )
{
String out;
int targetLength = s.length();
int replaceLength = rep.length();
int matchLength = find.length();
StringTokenizer st = new StringTokenizer( s, find );
int firstIndex = s.indexOf( find ); if ( firstIndex < 0 )
{
// 无匹配
out = s; return out;
} if ( firstIndex > 0 )
{
out = "";
}
else
{
// 以要替换的字符串开头
out = rep;
} while ( st.hasMoreTokens() )
{
out += st.nextToken() + rep;
} out = out.substring( 0, out.length() - replaceLength ); int lastIndex = s.lastIndexOf( find ); // 以要替换的字符串结尾
if ( lastIndex == ( targetLength - matchLength ) )
{
out = out + rep;
} return out;
} /**
* @author beh
*
* @ Modified by : Greg Karbett
*
* Replaces one substring with another within a main string.
*
* @param s Main string.
* @param find The substring to find.
* @param rep Replaces the find substring.
* @return The new string.
*/
public static String replaceStringBuffer( String s, String find,
String rep )
{
return replace( new StringBuffer( s ), find, rep ).toString();
}
/**
* @author beh
*
* @ Modified by : Greg Karbett
*
* Replaces one substring with another within a main string.
*
* @param sb Main string. (StringBuffer)
* @param find The substring to find.
* @param rep Replaces the find substring.
* @return The new string. (StringBuffer)
*/
private static StringBuffer replace( StringBuffer sb, String find,
String rep )
{
StringBuffer buf = new StringBuffer( sb.toString() );
int startIndex = 0;
int stringLocation = 0;
boolean done = false; // Halts if they is not need to do any replacements
if ( ! find.equals( rep ) )
{
String s = buf.toString(); // Continues while more substring(s) (find) exist
while ( ! done )
{
// Grab the position of the substring (find)
if ( ( stringLocation = s.indexOf( find, startIndex ) ) >= 0 )
{
// Replace "find" with "rep"
buf.delete( stringLocation, stringLocation +
find.length() );
buf.insert( stringLocation, rep );
startIndex = stringLocation + rep.length();
s = buf.toString();
}
else
{
done = true;
}
}
} return buf;
}
taozabc(Pluto)说的方法可行啊,没问题。
字符串"\\\""不就是等于实际上的\"吗?
我也测试通过了。
public class ReplaceChar
{
public static void main(String[] args) {
System.out.println(replace(args[0], "\"", "\\\""));
}
}使用命令: java ReplaceChar tttase"sdfae得到的结果是:tttasesdfae
把"丢掉了。
java ReplaceChar tttase"sdfae
这样运行的话,args[0]本身就等于tttasesdfae
若要args[0]为tttase"sdfae,则需要用命令
java ReplaceChar tttase\"sdfae
运行命令java ReplaceChar tttase"sdfae
此时args[0]本身就是tttasesdfae,如何能转换成功?
要想args[0]是tttase"sdfae,运行命令必须是
java ReplaceChar tttase\"sdfae才行
另外:String的replaceAll方法是jdk1.4才带有的
此时args[0]本身就是tttasesdfae”??谢谢1!!!