我试图理解Pattern.quote
使用以下代码:
String pattern = Pattern.quote("1252343% 8 567 hdfg gf^$545");
System.out.println("Pattern is : "+pattern);
Run Code Online (Sandbox Code Playgroud)
产生输出:
Pattern is : \Q1252343% 8 567 hdfg gf^$545\E
Run Code Online (Sandbox Code Playgroud)
什么是\Q
和\E
这里?文档说明说:
返回
String
指定的文字模式String
.此方法生成一个
String
可用于创建Pattern
与字符串匹配的字符s
,就好像它是文字模式一样.输入序列中的元字符或转义序列将没有特殊含义.
但是Pattern.quote
返回类型是String
而不是编译Pattern
对象.
为什么需要此方法以及一些用法示例?
任何人都可以帮我创建java中的变量的正则表达式,以便字符串变量将被视为不区分大小写,并用WINDOWS替换每个单词,如Access,Access等等吗?
这是代码:
$html=html.replaceAll(label, "WINDOWS");
Run Code Online (Sandbox Code Playgroud)
请注意,label是一个字符串变量.
我正在做一些初学者的编码练习,我遇到了这个问题:给定两个字符串,base和remove,返回一个基本字符串的版本,其中删除了删除字符串的所有实例.(不区分大小写).
这是我到目前为止所做的,但它根本不起作用.
public String withoutString(String base, String remove) {
for (int i=0; i<base.length()-remove.length(); i++){
if (base.substring(i, i+remove.length()).equals(remove)){
base = base.substring(i, base.indexOf("remove")-1) + base.substring(base.indexOf("remove"), base.length()-remove.length());
}
}
return base;
}
Run Code Online (Sandbox Code Playgroud)
我还没有处理区分大小写的部分,以使其对我自己更加明显.我也不确定为什么我不能使用base.replaceAll("remove",""); 任何帮助表示赞赏.
编辑*:我犯了一个新手错误,替换所有仍然有效.另外,我怎么能用循环和条件做到这一点?它会像我以前一样凌乱吗?
嗨朋友我正在创建一个应用程序.
我想找到一个特定的单词ArrayList
,我必须
用另一个单词替换它.我使用下面的代码.它的工作区分大小写,
但我想让它工作区不敏感.
FillintheBlank.class:
public class FillintheBlank extends Activity {
static ArrayList<String> multiword=new ArrayList<String>();
static ArrayList<String> multimeaning=new ArrayList<String>();
public void setNoTitle() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
float screenHeight,screenWidth,screendensity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setNoTitle();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
screendensity = displaymetrics.densityDpi;
setContentView(R.layout.fillinblanknew);
multiword.add("radha");
multiword.add("RAdHA");
multiword.add("latha");
multiword.add("mammu");
s.o.p(""+multiword);
// output:radha,RADHA,latha,mamu
multimeaning.add(multiword.getString().replace(radha,"sai"));
s.o.p(""+multimeaning);
// output: sai,RADHA,latha,mamu
}
}
Run Code Online (Sandbox Code Playgroud)
例如:无论'radha'中的字母是什么,我都需要用'sai'替换'radha'.
我想用另一个字符串替换字符串的一部分.例如,一个字符串进来,它包含meat
,但我希望它将其更改meat
为vegetable
.我知道我可以用以下声明来做到这一点.
str = str.replaceAll("meat", "vegetable");
但是,我需要具有确切的大小写.我正在寻找一种方法来执行此操作,无论示例中的大写字母是大写还是小写meat
.
我的应用程序是Minecraft的过滤器.要么发誓,要么只是定制菜单.就像我想要替换这个词的每个实例一样minecraft
,无论大写,都要说Best game Ever!!
.
我已对其进行了修改,以便它可以识别并替换特定的大写字母,但这是非常有限的.
我希望我提供了足够的信息,以便有人可以帮助我.
String Checkout = D:\ifs\APP\Checkout
String DeleteLine = D:\IFS\APP\Checkout\trvexp\client\Ifs.App\text.txt
Run Code Online (Sandbox Code Playgroud)
注意两个字符串中的ifs和IFS.我想更换结帐字符串中删除订单
所以最终的String看起来像这样:
\trvexp\client\Ifs.App\text.txt
Run Code Online (Sandbox Code Playgroud)
以下是我尝试过的,但显然由于Case Sensitivity,字符串不会被替换.任何解决方案或解决此问题的方法?
String final = DeleteLine.replace(Checkout, "");
Run Code Online (Sandbox Code Playgroud) 程序需要计算并显示指定的charector在文本文件中出现的次数.
目前总数为零.我不是,如果我应该使用不同的循环,我也尝试使用'for'循环.
// Hold user input and sum
String fileName; // Holds the name of the file
String letter; // Letter to search for in the file
int total = 0; // Holds the total number of characters in the file
// Get the name of the file and character from the user
fileName = JOptionPane.showInputDialog("Please enter the name of a file:");
letter = JOptionPane.showInputDialog("Please enter a letter contained in the string");
// Open the file for reading
File file …
Run Code Online (Sandbox Code Playgroud)