Java从多行String中获取子串或拆分

时间:2018-03-13 19:24:56

标签: java

我想从一个由多行组成的字符串中获取一个字符串

示例字符串:

Date: May 12, 20003
ID: 54076
Content: Test
filename: Testing.fileextension
folder: Test Folder
endDate: May 13, 2003

我想要的输出是"Testing.filextension" 我尝试了几种方法,但没有一种只返回文件名。

方法尝试1:

String[] files = example.substring(example.indexOf("filename: ")
for (String filename : files){
    System.out.printlin(filename);
}

方法尝试2是尝试拆分换行符,但是现在它只返回所有行。我的主要问题是.split方法需要(String, int),但是,我没有int。

String[] files = example.split("\\r?\\n");
for (String filename : files){
    System.out.println(filename)
}

7 个答案:

答案 0 :(得分:2)

使用流怎么样?您应该考虑过滤无法拆分的行,例如当分裂角色丢失时。

所以试试这个:

Map<String, String> myMap = Arrays.stream(myString.split("\\r?\\n"))
            .map(string -> string.split(": "))
            .collect(Collectors.toMap(stringArray -> stringArray[0], 
                    stringArray -> stringArray[1]));

    System.out.println(myMap.get("filename"));

答案 1 :(得分:1)

还有其他限制吗?如果不只是与if:

结合使用
String keyword = "filename: ";
String[] files = example.split("\\r?\\n");
for(String file : files) {
    if(file.startsWith(keyword)){
        System.out.println(file.substring(keyword.length()));
    }
}

答案 2 :(得分:0)

你可以这样过滤它:

String[] files = example.split("\\r?\\n");
for (String filename : files){
    if (filename.startsWith("filename"))
        System.out.println(filename);
}

答案 3 :(得分:0)

使用

String[] files = example.split("\\r?\\n");
for (String filename : files){
    if (filename.startsWith("filename"))
        System.out.println(filename);
}

如果你没有fileextension那就没有文件名

String[] files = example.split("\\r?\\n");
for (String filename : files){
    if (filename.startsWith("filename"))
        System.out.println(filename.split(".")[0]);
}

答案 4 :(得分:0)

试试这个:

String searchStr = "filename:";
example.substring(example.indexOf(searchStr) + searchStr.length()).split("\n")[0].trim();

虽然我建议你将输入结构化为HashMap,这样以后会更容易使用。

答案 5 :(得分:0)

/**
 * @Author Jack <J@ck>
 * https://stackoverflow.com/questions/49264343/java-get-substring-or-split-from-multiline-string
 */
public class StringFromStringComposedOfManyLines {
    public static void main(String[] args) {
        String manyLinesString =
                "Date: May 12, 20003\n" +
                        "ID: 54076\n" +
                        "Content: Test\n" +
                        "filename: Testing.fileextension\n" +
                        "folder: Test Folder\n" +
                        "endDate: May 13, 2003\n";
        System.out.println(manyLinesString);

        // Replace all \n \t \r occurrences to "" and gets one line string
        String inLineString = manyLinesString.replaceAll("\\n|\\t||\\r", "");
        System.out.println(inLineString);
    }
}
  

Out:Date:2000年5月12日IDID:54076内容:Testfilename:   Testing.fileextensionfolder:Test FolderendDate:2003年5月13日

答案 6 :(得分:0)

我建议使用正则表达式和命名组,如下所示:

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class RegExTest {

    public static void main(String[] args) {

        // Your example string
        String exampleStr = "Date: May 12, 20003\nID: 54076\nContent: Test\nfilename: Testing.fileextension\nfolder: Test Folder\nendDate: May 13, 2003.";

        /* create the regex:
         * you are providing a regex group named fname
         * This code assumes that the file name ends with a newline/eof.
         * the name of the group that will match the filename is "fname".
         * The brackets() indicate that this is a named group.
         */
        String regex = "filename:\\s*(?<fname>[^\\r\\n]+)";

        // create a pattern object by compiling the regex
        Pattern pattern = Pattern.compile(regex);

        // create a matcher object by applying the pattern on the example string:
        Matcher matcher = pattern.matcher(exampleStr);

        // if a match is found
        if (matcher.find()) {
            System.out.println("Found a match:");
            // output the content of the group that matched the filename:
            System.out.println(matcher.group("fname"));
        }
        else {
            System.out.println("No match found");
        }
    }
}