How do you replace strings in ?

2008-12-08 7:37 am
For example: Given two strings, base and remove, return a version of the base string where all instances of the remove string have been removed (not case sensitive). You may assume that the remove string is length 1 or more. Remove only non-overlapping instances, so with "xxx" removing "xx" leaves "x". Could you just use the replace command? If so, how? If not... please help me... an hour and a half with no progress makes an unhappy person. Thanks for any help
更新1:

Replacing in java, sorry for not claifying

回答 (1)

2008-12-08 7:44 am
✔ 最佳答案
/*
Here is the sample code. See if this is what you wanted.
Have a nice day!
*/
import java.io.*;
import java.util.regex.*;

class Chead
{
public static void main(String[] args)
{
String strHay = null;
Pattern pttnNeedle = null;
Matcher mtchrNeedle = null;
String strResult = null;

// generate a case-insensitive regexp pattern from the 1st param
if (args.length > 0)
{
pttnNeedle = Pattern.compile
(
args[0]
, Pattern.CASE_INSENSITIVE
);
}

// loop through other parameters and removes the matched strings
for(int i = 1; i < args.length; i ++)
{
strHay = args[i];
mtchrNeedle = pttnNeedle.matcher(strHay);
strResult = mtchrNeedle.replaceAll("");
System.out.println("The result is:\n\t" + strResult);
}

return;
}
}


收錄日期: 2021-05-01 00:59:58
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20081207233704AAML1wD

檢視 Wayback Machine 備份