3 回答
TA贡献1848条经验 获得超6个赞
由于输入字符串中没有固定模式,您必须编写自己的解析器,或者可以使用不同的正则表达式来获取不同的参数。对于例如 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贡献1785条经验 获得超8个赞
首先删除您"和;从字符串,其次,他们都拆你想检索,如术语filename,size通过新的阵列等...,循环,并通过将它们分割:和=。最后只需将它们放入 aHashMap以便像那样检索它们,map.get("filename"). 请参阅下面的解决方案。
编辑:当您要求创建一个ArrayList<String>以收集同一键下的所有值时,我将其更新如下。
注意:为了不filename与分开name,我将name一个空格作为术语。
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贡献1817条经验 获得超6个赞
如果您已经知道主字符串的基本格式和内容样式,那么您可以使用自定义子字符串检索方法来获取所需的数据。我在下面提供的方法允许您检索包含在其他两个子字符串之间的子字符串,例如:
如果您想检索与子字符串“filename=”(当然是“mozilla.pdf”)相关的文件名,那么您可以为该方法提供一个 Left-String of"filename=\""和一个 Right-String of "\""。
该方法返回任何出现的一维字符串数组,其中提供的左子字符串和右子字符串之间可能有一个子字符串,因此对于上面的示例,我们将调用该方法,如下所示:
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]);
}
这最终将在主输入字符串中找到的所有文件名打印到控制台窗口。如果您只想要文件名的第一个实例,那么您可以在方法调用的末尾放置一个索引值以检索所需的文件名,例如:
String fileName = getSubstring(inputString,"filename=\"", "\"")[0];
System.out.println("File Name:\t" + fileName);
这将打印:File Name: mozilla.pdf到控制台窗口。
这是方法:
/**
* 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;
}
要检索您提供的字符串中包含的数据:
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]);
控制台的输出将是:
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
添加回答
举报