获取分配给长字符串中变量的值

我有一个从数据库传递过来的字符串,它本质上是一个电子邮件正文,包含如下内容:

内容类型:应用程序/pdf;name="mozilla.pdf" 内容描述:mozilla.pdf 内容处理:附件;文件名=“mozilla.pdf”;大小=92442;创建日期=“星期五,2018 年 10 月 12 日 14:14:00 GMT”;修改日期="星期五,2018 年 10 月 12 日 14:14:00 GMT"内容传输编码:base64"

我希望能够获取文件名内容类型等。

例如:从上面的文本文件名将是mozilla.pdf


繁华开满天机
浏览 150回答 3
3回答

慕勒3428872

由于输入字符串中没有固定模式,您必须编写自己的解析器,或者可以使用不同的正则表达式来获取不同的参数。对于例如 fetch filename,您可以使用:final String regex = "filename=\"(.*?)\";";final Pattern pattern = Pattern.compile(regex);final Matcher matcher = pattern.matcher(<input-string>);if (matcher.find()) {&nbsp; &nbsp; System.out.println("Filename: " + matcher.group(1));}

慕的地10843

首先删除您"和;从字符串,其次,他们都拆你想检索,如术语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) {&nbsp; String[] keyValue = part.split("[:=]");&nbsp; String key = keyValue[0].trim();&nbsp; String value = keyValue[1].trim();&nbsp; ArrayList<String> list;&nbsp; if(map.containsKey(key)){&nbsp; &nbsp; list = map.get(key);&nbsp; &nbsp; list.add(value);&nbsp; } else {&nbsp; &nbsp; list = new ArrayList<String>();&nbsp; &nbsp; list.add(value);&nbsp; &nbsp; map.put(key, list);&nbsp; }}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]

慕的地6264312

如果您已经知道主字符串的基本格式和内容样式,那么您可以使用自定义子字符串检索方法来获取所需的数据。我在下面提供的方法允许您检索包含在其他两个子字符串之间的子字符串,例如:如果您想检索与子字符串“filename=”(当然是“mozilla.pdf”)相关的文件名,那么您可以为该方法提供一个 Left-String of"filename=\""和一个 Right-String of "\""。该方法返回任何出现的一维字符串数组,其中提供的左子字符串和右子字符串之间可能有一个子字符串,因此对于上面的示例,我们将调用该方法,如下所示:String inputString = "Content-Type: application/pdf; name=\"mozilla.pdf\" "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ "Content-Description: mozilla.pdf Content-Disposition: attachment; "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ "filename=\"mozilla.pdf\"; size=92442; creation-date=\""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ "Fri, 12 Oct 2018 14:14:00 GMT\"; modification-date=\""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;+ "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++) {&nbsp; &nbsp; 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:&nbsp; &nbsp; &nbsp;mozilla.pdf到控制台窗口。这是方法:/**&nbsp;* Retrieves any string data located between the supplied string leftString&nbsp;* parameter and the supplied string rightString parameter.<br><br>&nbsp;*&nbsp;&nbsp;* It can also retrieve a substring located at the beginning or the end of&nbsp;&nbsp;* the main input string (see: leftString and rightString parameter information).&nbsp;*&nbsp;&nbsp;* <p>&nbsp;* This method will return all instances of a substring located between the&nbsp;* supplied Left String and the supplied Right String which may be found&nbsp;* within the supplied Input String.<br>&nbsp;*&nbsp;* @param inputString (String) The string to look for substring(s) in.&nbsp;*&nbsp;* @param leftString&nbsp; (String) What may be to the Left side of the substring&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; we want within the main input string. Sometimes the&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; substring you want may be contained at the very beginning&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; of a string and therefore there is no Left-String available.&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; In this case you would simply pass a Null String ("") to&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this parameter which basically informs the method of this&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fact. Null can not be supplied and will ultimately generate&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a NullPointerException. If a Null String ("") is supplied&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then the rightString parameter <b>must</b> contain a String.&nbsp;*&nbsp;* @param rightString (String) What may be to the Right side of the&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; substring we want within the main input string.&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sometimes the substring you want may be contained&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; at the very end of a string and therefore there is&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; no Right-String available. In this case you would&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; simply pass a Null String ("") to this parameter&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; which basically informs the method of this fact.&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Null can not be supplied and will ultimately generate&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a NullPointerException. If a Null String ("") is supplied&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then the leftString parameter <b>must</b> contain a String.&nbsp;*&nbsp;&nbsp;* @param options&nbsp; &nbsp; &nbsp;(Optional - Boolean - 2 Parameters):<pre>&nbsp;*&nbsp;*&nbsp; &nbsp; &nbsp; ignoreLetterCase&nbsp; &nbsp; - Default is false. This option works against the&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string supplied within the leftString parameter&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; and the string supplied within the rightString&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parameter. If set to true then letter case is&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ignored when searching for strings supplied in&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; these two parameters. If left at default false&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; then letter case is not ignored.&nbsp;&nbsp;*&nbsp;*&nbsp; &nbsp; &nbsp; trimFound&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;- Default is true. By default this method will trim&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; off leading and trailing white-spaces from found&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sub-string items. General sentences which obviously&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contain spaces will almost always give you a white-&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; space within an extracted sub-string. By setting&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this parameter to false, leading and trailing white-&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spaces are not trimmed off before they are placed&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; into the returned Array.</pre>&nbsp;*&nbsp;* @return (1D String Array) Returns a Single Dimensional String Array&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;containing all the sub-strings found within the supplied Input&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String which are between the supplied Left String and supplied&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Right String. Returns Null if nothing is found.&nbsp;*&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;You can shorten this method up a little by returning a List&lt;String&gt;&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ArrayList and removing the 'List to 1D Array' conversion code at&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;the end of this method. This method initially stores its findings&nbsp;&nbsp;*&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;within a List Interface object anyways.&nbsp;*/public static String[] getSubstring(String inputString, String leftString, String rightString, boolean... options) {&nbsp; &nbsp; // Return nothing if nothing was supplied.&nbsp; &nbsp; if (inputString.equals("") || (leftString.equals("") && rightString.equals(""))) {&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; // Prepare optional parameters if any supplied.&nbsp; &nbsp; // If none supplied then use Defaults...&nbsp; &nbsp; boolean ignoreCase = false; // Default.&nbsp; &nbsp; boolean trimFound = true;&nbsp; &nbsp;// Default.&nbsp; &nbsp; if (options.length > 0) {&nbsp; &nbsp; &nbsp; &nbsp; if (options.length >= 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ignoreCase = options[0];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (options.length >= 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; trimFound = options[1];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Remove any ASCII control characters from the&nbsp; &nbsp; // supplied string (if they exist).&nbsp; &nbsp; String modString = inputString.replaceAll("\\p{Cntrl}", "");&nbsp; &nbsp; // Establish a List String Array Object to hold&nbsp; &nbsp; // our found substrings between the supplied Left&nbsp; &nbsp; // String and supplied Right String.&nbsp; &nbsp; List<String> list = new ArrayList<>();&nbsp; &nbsp; // Use Pattern Matching to locate our possible&nbsp; &nbsp; // substrings within the supplied Input String.&nbsp; &nbsp; String regEx = Pattern.quote(leftString) +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(!rightString.equals("") ? "(.*?)" : "(.*)?") +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Pattern.quote(rightString);&nbsp; &nbsp; if (ignoreCase) {&nbsp; &nbsp; &nbsp; &nbsp; regEx = "(?i)" + regEx;&nbsp; &nbsp; }&nbsp; &nbsp; Pattern pattern = Pattern.compile(regEx);&nbsp; &nbsp; Matcher matcher = pattern.matcher(modString);&nbsp; &nbsp; while (matcher.find()) {&nbsp; &nbsp; &nbsp; &nbsp; // Add the found substrings into the List.&nbsp; &nbsp; &nbsp; &nbsp; String found = matcher.group(1);&nbsp; &nbsp; &nbsp; &nbsp; if (trimFound) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found = found.trim();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; list.add(found);&nbsp; &nbsp; }&nbsp; &nbsp; String[] res;&nbsp; &nbsp; // Convert the ArrayList to a 1D String Array.&nbsp; &nbsp; // If the List contains something then convert&nbsp; &nbsp; if (list.size() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; res = new String[list.size()];&nbsp; &nbsp; &nbsp; &nbsp; res = list.toArray(res);&nbsp; &nbsp; } // Otherwise return Null.&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; res = null;&nbsp; &nbsp; }&nbsp; &nbsp; // Return the String Array.&nbsp; &nbsp; 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:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;application/pdfName:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;mozilla.pdfContent-Description:&nbsp; &nbsp; &nbsp; &nbsp; mozilla.pdfContent-Disposition:&nbsp; &nbsp; &nbsp; &nbsp; attachmentFile Name:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mozilla.pdfFile Size:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 92442Creation Date:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fri, 12 Oct 2018 14:14:00 GMTModification Date:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fri, 12 Oct 2018 14:14:00 GMTContent Transfer Encoding&nbsp; &nbsp;base64
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java