如何在java中逐字比较两个字符串
我的代码:如何在java中逐字比较两个字符串,java,android,Java,Android,我的代码: String[] EtChArray = etValue.split(""); String[] VtChArray = fullStory.split(""); if (EtChArray[i].isEmpty()) { i++; } else { if (EtChArray[i].equals(VtChArray[i])) { tview1.setText(EtChArray[i]); tview1.setTextColor(Color.
String[] EtChArray = etValue.split("");
String[] VtChArray = fullStory.split("");
if (EtChArray[i].isEmpty()) {
i++;
} else {
if (EtChArray[i].equals(VtChArray[i])) {
tview1.setText(EtChArray[i]);
tview1.setTextColor(Color.GREEN);
i++;
} else {
tview1.setText(EtChArray[i]);
tview1.setTextColor(Color.RED);
i++;
}
}
我想比较android studio java中的编辑文本和查看文本。我想在编辑文本框中输入文本,并将其与viewtext预定义的文本字符进行实时比较,但在兼容空格时遇到问题 您正在使用空字符串拆分字符串。我想应该是etValue.split和fullStory.split.String.splitString采用正则表达式。一个或多个连续空格的正则表达式为\s+。在Java中,您必须转义\,因此您将编写字符串[]EtChArray=etValue.split\\s+;字符串[]VtChArray=fullStory.split\\s+;如果您有前导和/或尾随空格,请在拆分之前进行修剪;s2=你好,我很忙;现在如何逐字比较这两个字符串我正在android上开发一个应用程序。我有一个编辑文本框和一个查看文本框。我想在编辑文本框中输入文本,并将其与文本视图文本进行实时比较,如果anyifmEdit.length>0{ifmEdit.toString.equalsIgnoreCasetvText.getText.toString.substring0,mEdit.length{TveEdit.setTextColorColor.GREEN;}else,则突出显示单词错误{tvEdit.setTextColorColor.RED;}上面的代码在任何键入的单词不正确时将整个文本颜色更改为红色。我只希望不正确的单词颜色为红色。ifmEdit.length>0{ifmEdit.toString.equalsIgnoreCasetvText.getText.toString.substring0,mEdit.length{tvEdit.setTextColorColor.GREEN;}else{tvEdit.setTextColorColor.RED;}当任何键入的单词不正确时,上述代码将整个文本颜色更改为红色。我只希望不正确的单词被涂成红色。
String s1 = "Hello world I am showkat";
String s2 = "Hello world I was busy";
String s1_words[] = s1.split("\\s");
String s2_words[] = s2.split("\\s");
int num_words = s1_words.length;
if (num_words == s2_words.length) {
for (int i = 0; i < num_words; i++) {
System.out.print("word " + (i+1) + ": " + s1_words[i].equals(s2_words[i]) + "\n");
}
}
word 1: true
word 2: true
word 3: true
word 4: false
word 5: false
// Text View for Comparison
final TextView tvText = (TextView) findViewById(R.id.textView);
// Edit text View where you will type text
final EditText tvEdit = (EditText) findViewById(R.id.editText);
// Listener for checking change in text
tvEdit.addTextChangedListener(new TextWatcher()
{
@Override
public void afterTextChanged(Editable mEdit)
{
if(mEdit.length() > 0){
// Equals Ignore case if you dont want to check on the basis of case
// This will check whether the typed string and your saved string upto that point match or not
if(mEdit.toString().equalsIgnoreCase(tvText.getText().toString().substring(0,mEdit.length()))){
tvEdit.setTextColor(Color.GREEN);
}else{
tvEdit.setTextColor(Color.RED);
}
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
}