Java 如何删除特定字符后arraylist元素中的文本?

Java 如何删除特定字符后arraylist元素中的文本?,java,arraylist,Java,Arraylist,我试图引入html验证错误并去掉错误的第一部分,以便只显示实际的文本部分,但我遇到了问题。我想删除文本后面的“ValidationError第23行第40列:”,以及最后一个“” package htmlvalidator; import java.util.ArrayList; public class ErrorCleanup { public static void main(String[] args) { //Saving the raw errors to an arr

我试图引入html验证错误并去掉错误的第一部分,以便只显示实际的文本部分,但我遇到了问题。我想删除文本后面的“ValidationError第23行第40列:”,以及最后一个“”

package htmlvalidator;

import java.util.ArrayList;

public class ErrorCleanup {

public static void main(String[] args) {
    //Saving the raw errors to an array list
    ArrayList<String> list = new ArrayList<String>();

    //Add the text to the first spot
    list.add("ValidationError line 23 col 40:'Bad value ius-cors for attribute name on element >meta: Keyword ius-cors is not registered.'");

    //Show what is in the list
    System.out.println("The error message is: " + list);

}

}
package-htmlvalidator;
导入java.util.ArrayList;
公共类错误清理{
公共静态void main(字符串[]args){
//将原始错误保存到数组列表
ArrayList=新建ArrayList();
//将文本添加到第一个点
添加(“ValidationError第23行第40列:'element>meta:关键字ius cors未注册的属性名称的错误值ius cors'”;
//显示列表中的内容
System.out.println(“错误消息为:“+list”);
}
}

简单但不灵活的方法是使用该方法

String fullText = list.get(0);                              // get the full text  
String msg = fullText.substring(32, fullText.length() - 1); // extract the substring you need
System.out.println("The error message is: " + msg);         // print the msg
如果您知道您的消息总是在单引号之间,您可以创建一个helper方法来提取它,如下所示:

// get first occurrence of a substring between single quotes
String getErrorMsg(String text) {
    StringBuilder msg = new StringBuilder();
    int index = 0;
    boolean matchingQuotes = false;      // flag to make sure we matched the quotes
    while(index < text.length()) {      
        if(text.charAt(index) == '\'') { // find the first single quote
            index++;                     // skip the first single quote
            break;
        }
        index++;
    }
    while(index < text.length()) {
        if(text.charAt(index) == '\'') { // find the second single quote
            matchingQuotes = true;       // set the flag to indicate the quotes were matched
            break;
        } else {
            msg.append(text.charAt(index)); 
        }
        index++;
    }
    if(matchingQuotes) {                 // if quotes were matched, return substring between them
        return msg.toString();
    } 
    return "";                           // if reached this point, no valid substring between single quotes
}
另一种方法是使用正则表达式


这里有一个

我不太确定你的错误会是什么样子。但接受每行第一个
之后发生的所有内容是否有效?如果是这样,您可以使用
String
类的
split
方法。
String fullText = list.get(0);                      // get the full text  
String msg = getErrorMsg(fullText);                 // extract the substring between single quotes
System.out.println("The error message is: " + msg); // print the msg