3 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超6個(gè)贊
由于輸入字符串中沒(méi)有固定模式,您必須編寫(xiě)自己的解析器,或者可以使用不同的正則表達(dá)式來(lái)獲取不同的參數(shù)。對(duì)于例如 fetch filename,您可以使用:
final String regex = "filename=\"(.*?)\";";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(<input-string>);
if (matcher.find()) {
System.out.println("Filename: " + matcher.group(1));
}

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊
首先刪除您"和;從字符串,其次,他們都拆你想檢索,如術(shù)語(yǔ)filename,size通過(guò)新的陣列等...,循環(huán),并通過(guò)將它們分割:和=。最后只需將它們放入 aHashMap以便像那樣檢索它們,map.get("filename"). 請(qǐng)參閱下面的解決方案。
編輯:當(dāng)您要求創(chuàng)建一個(gè)ArrayList<String>以收集同一鍵下的所有值時(shí),我將其更新如下。
注意:為了不filename與分開(kāi)name,我將name一個(gè)空格作為術(shù)語(yǔ)。
String string = "Content-Type: application/pdf; name=\"mozilla.pdf\" name=\"mozilla2.pdf\" name=\"mozilla3.pdf\" Content-Description: mozilla.pdf Content-Disposition: attachment; filename=\"mozilla.pdf\"; size=92442; creation-date=\"Fri, 12 Oct 2018 14:14:00 GMT\"; modification-date=\"Fri, 12 Oct 2018 14:14:00 GMT\"Content-Transfer-Encoding: base64";
string = string.replaceAll("[\";]", "");
String[] parts = string.split("(?=(Content-Type)|( name)|(Content-Description)|(Content-Disposition)|(filename)|(size)|(creation-date)|(modification-date)|(Content-Transfer-Encoding))");
Map<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
for (String part : parts) {
String[] keyValue = part.split("[:=]");
String key = keyValue[0].trim();
String value = keyValue[1].trim();
ArrayList<String> list;
if(map.containsKey(key)){
list = map.get(key);
list.add(value);
} else {
list = new ArrayList<String>();
list.add(value);
map.put(key, list);
}
}
System.out.println(map.get("name"));
System.out.println(map.get("Content-Type"));
System.out.println(map.get("filename"));
System.out.println(map.get("creation-date"));
System.out.println(map.get("size"));
輸出
[mozilla.pdf, mozilla2.pdf, mozilla3.pdf]
[application/pdf]
[mozilla.pdf]
[Fri, 12 Oct 2018 14]
[92442]

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊
如果您已經(jīng)知道主字符串的基本格式和內(nèi)容樣式,那么您可以使用自定義子字符串檢索方法來(lái)獲取所需的數(shù)據(jù)。我在下面提供的方法允許您檢索包含在其他兩個(gè)子字符串之間的子字符串,例如:
如果您想檢索與子字符串“filename=”(當(dāng)然是“mozilla.pdf”)相關(guān)的文件名,那么您可以為該方法提供一個(gè) Left-String of"filename=\""和一個(gè) Right-String of "\""。
該方法返回任何出現(xiàn)的一維字符串?dāng)?shù)組,其中提供的左子字符串和右子字符串之間可能有一個(gè)子字符串,因此對(duì)于上面的示例,我們將調(diào)用該方法,如下所示:
String inputString = "Content-Type: application/pdf; name=\"mozilla.pdf\" "
+ "Content-Description: mozilla.pdf Content-Disposition: attachment; "
+ "filename=\"mozilla.pdf\"; size=92442; creation-date=\""
+ "Fri, 12 Oct 2018 14:14:00 GMT\"; modification-date=\""
+ "Fri, 12 Oct 2018 14:14:00 GMT\"Content-Transfer-Encoding: base64";
String[] fileNames = getSubstring(inputString,"filename=\"", "\"");
for (int i = 0; i < fileNames.length; i++) {
System.out.println("File Name " + (i+1) + ":\t" + fileNames[i]);
}
這最終將在主輸入字符串中找到的所有文件名打印到控制臺(tái)窗口。如果您只想要文件名的第一個(gè)實(shí)例,那么您可以在方法調(diào)用的末尾放置一個(gè)索引值以檢索所需的文件名,例如:
String fileName = getSubstring(inputString,"filename=\"", "\"")[0];
System.out.println("File Name:\t" + fileName);
這將打?。篎ile Name: mozilla.pdf到控制臺(tái)窗口。
這是方法:
/**
* Retrieves any string data located between the supplied string leftString
* parameter and the supplied string rightString parameter.<br><br>
*
* It can also retrieve a substring located at the beginning or the end of
* the main input string (see: leftString and rightString parameter information).
*
* <p>
* This method will return all instances of a substring located between the
* supplied Left String and the supplied Right String which may be found
* within the supplied Input String.<br>
*
* @param inputString (String) The string to look for substring(s) in.
*
* @param leftString (String) What may be to the Left side of the substring
* we want within the main input string. Sometimes the
* substring you want may be contained at the very beginning
* of a string and therefore there is no Left-String available.
* In this case you would simply pass a Null String ("") to
* this parameter which basically informs the method of this
* fact. Null can not be supplied and will ultimately generate
* a NullPointerException. If a Null String ("") is supplied
* then the rightString parameter <b>must</b> contain a String.
*
* @param rightString (String) What may be to the Right side of the
* substring we want within the main input string.
* Sometimes the substring you want may be contained
* at the very end of a string and therefore there is
* no Right-String available. In this case you would
* simply pass a Null String ("") to this parameter
* which basically informs the method of this fact.
* Null can not be supplied and will ultimately generate
* a NullPointerException. If a Null String ("") is supplied
* then the leftString parameter <b>must</b> contain a String.
*
* @param options (Optional - Boolean - 2 Parameters):<pre>
*
* ignoreLetterCase - Default is false. This option works against the
* string supplied within the leftString parameter
* and the string supplied within the rightString
* parameter. If set to true then letter case is
* ignored when searching for strings supplied in
* these two parameters. If left at default false
* then letter case is not ignored.
*
* trimFound - Default is true. By default this method will trim
* off leading and trailing white-spaces from found
* sub-string items. General sentences which obviously
* contain spaces will almost always give you a white-
* space within an extracted sub-string. By setting
* this parameter to false, leading and trailing white-
* spaces are not trimmed off before they are placed
* into the returned Array.</pre>
*
* @return (1D String Array) Returns a Single Dimensional String Array
* containing all the sub-strings found within the supplied Input
* String which are between the supplied Left String and supplied
* Right String. Returns Null if nothing is found.
*
* You can shorten this method up a little by returning a List<String>
* ArrayList and removing the 'List to 1D Array' conversion code at
* the end of this method. This method initially stores its findings
* within a List Interface object anyways.
*/
public static String[] getSubstring(String inputString, String leftString, String rightString, boolean... options) {
// Return nothing if nothing was supplied.
if (inputString.equals("") || (leftString.equals("") && rightString.equals(""))) {
return null;
}
// Prepare optional parameters if any supplied.
// If none supplied then use Defaults...
boolean ignoreCase = false; // Default.
boolean trimFound = true; // Default.
if (options.length > 0) {
if (options.length >= 1) {
ignoreCase = options[0];
}
if (options.length >= 2) {
trimFound = options[1];
}
}
// Remove any ASCII control characters from the
// supplied string (if they exist).
String modString = inputString.replaceAll("\\p{Cntrl}", "");
// Establish a List String Array Object to hold
// our found substrings between the supplied Left
// String and supplied Right String.
List<String> list = new ArrayList<>();
// Use Pattern Matching to locate our possible
// substrings within the supplied Input String.
String regEx = Pattern.quote(leftString) +
(!rightString.equals("") ? "(.*?)" : "(.*)?") +
Pattern.quote(rightString);
if (ignoreCase) {
regEx = "(?i)" + regEx;
}
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(modString);
while (matcher.find()) {
// Add the found substrings into the List.
String found = matcher.group(1);
if (trimFound) {
found = found.trim();
}
list.add(found);
}
String[] res;
// Convert the ArrayList to a 1D String Array.
// If the List contains something then convert
if (list.size() > 0) {
res = new String[list.size()];
res = list.toArray(res);
} // Otherwise return Null.
else {
res = null;
}
// Return the String Array.
return res;
}
要檢索您提供的字符串中包含的數(shù)據(jù):
System.out.println("Content-Type:\t\t\t" + getSubstring(inputString,"Content-Type:", ";")[0]);
System.out.println("Name:\t\t\t\t" + getSubstring(inputString,"name=\"", "\"")[0]);
System.out.println("Content-Description:\t\t" + getSubstring(inputString,"Content-Description:", "Content-Disposition:")[0]);
System.out.println("Content-Disposition:\t\t" + getSubstring(inputString,"Content-Disposition:", ";")[0]);
System.out.println("File Name:\t\t\t" + getSubstring(inputString,"filename=\"", "\"")[0]);
System.out.println("File Size:\t\t\t" + getSubstring(inputString,"size=", ";")[0]);
System.out.println("Creation Date:\t\t\t" + getSubstring(inputString,"creation-date=\"", "\";")[0]);
System.out.println("Modification Date:\t\t" + getSubstring(inputString,"modification-date=\"", "\"")[0]);
System.out.println("Content Transfer Encoding\t" + getSubstring(inputString,"Content-Transfer-Encoding:", "")[0]);
控制臺(tái)的輸出將是:
Content-Type: application/pdf
Name: mozilla.pdf
Content-Description: mozilla.pdf
Content-Disposition: attachment
File Name: mozilla.pdf
File Size: 92442
Creation Date: Fri, 12 Oct 2018 14:14:00 GMT
Modification Date: Fri, 12 Oct 2018 14:14:00 GMT
Content Transfer Encoding base64
添加回答
舉報(bào)