[英]Java get substring or split from multiline String
我想從由多行組成的字符串中獲取一個字符串
示例字符串:
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)
}
怎樣使用流? 您應該考慮過濾無法拆分的行,例如,缺少拆分字符時。
所以試試這個:
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"));
還有其他限制嗎? 如果不是,則僅與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()));
}
}
您可以通過以下方式對其進行過濾:
String[] files = example.split("\\r?\\n");
for (String filename : files){
if (filename.startsWith("filename"))
System.out.println(filename);
}
采用
String[] files = example.split("\\r?\\n");
for (String filename : files){
if (filename.startsWith("filename"))
System.out.println(filename);
}
如果沒有文件擴展名就不要文件名
String[] files = example.split("\\r?\\n");
for (String filename : files){
if (filename.startsWith("filename"))
System.out.println(filename.split(".")[0]);
}
嘗試這個:
String searchStr = "filename:";
example.substring(example.indexOf(searchStr) + searchStr.length()).split("\n")[0].trim();
盡管我建議您將輸入結構化為HashMap,但以后使用起來會更容易。
/**
* @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);
}
}
出:日期:20003年5月12日ID:54076內容:測試文件名:Testing.fileextension文件夾:測試文件夾結束日期:2003年5月13日
我建議使用正則表達式和命名組,如下所示:
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");
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.