Java中如何截取字符串特定部分?提取VIN段内容至List<String>
Alright, let's solve this string extraction problem. You need to pull the VIN codes (from A9 to Z5 in your example) into a List<String>, and since the codes themselves can change but the VIN and CODES markers are fixed, we can use pattern-based approaches that don't rely on hardcoding the codes. Here are two reliable methods, using Java as the example language since you mentioned List<String>:
Method 1: Regular Expressions (Flexible for Variable Formatting)
Regex is great here because it can handle minor variations in spacing around the markers. We'll create a pattern that captures everything between VIN : and the next - CODES : segment, then split that captured content into individual codes.
import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class VinCodeExtractor { public static void main(String[] args) { String input = "- VIN : A9 C1 C4 C5 C6 E9 F2 F5 F8 G1 G7 H4 H6 I1 I4 I5 I6 I7 J1 K8 L3 L5 M4 O2 P6 Q9 R3 S3 T9 U3 V2 V3 V9 W5 Z5 - CODES : A9 C1 C4 C5 C6 E9 F2 F5 F8 G1 G7 H4 H6 I1 I4 I5 I6 I7 J1 K8 L3 L5 M4 O2 P6 Q9 R3 S3 T9 U3 V2 V3 V9 W5 Y4 Y8"; // Regex pattern to capture the VIN section between the fixed markers Pattern vinPattern = Pattern.compile("VIN : (.*?) - CODES :"); Matcher matcher = vinPattern.matcher(input); List<String> vinCodes = new ArrayList<>(); if (matcher.find()) { // Get the full VIN code string String vinSection = matcher.group(1); // Split by one or more spaces, trim to avoid empty strings String[] codeArray = vinSection.trim().split("\\s+"); for (String code : codeArray) { vinCodes.add(code); } } // Output the result System.out.println("Extracted VIN Codes: " + vinCodes); } }
Method 2: String Splitting (Simple for Well-Formatted Input)
If your input string always follows the exact structure you shared, splitting the string directly on the fixed markers is straightforward and easy to read.
import java.util.ArrayList; import java.util.List; public class VinCodeExtractorSplit { public static void main(String[] args) { String input = "- VIN : A9 C1 C4 C5 C6 E9 F2 F5 F8 G1 G7 H4 H6 I1 I4 I5 I6 I7 J1 K8 L3 L5 M4 O2 P6 Q9 R3 S3 T9 U3 V2 V3 V9 W5 Z5 - CODES : A9 C1 C4 C5 C6 E9 F2 F5 F8 G1 G7 H4 H6 I1 I4 I5 I6 I7 J1 K8 L3 L5 M4 O2 P6 Q9 R3 S3 T9 U3 V2 V3 V9 W5 Y4 Y8"; List<String> vinCodes = new ArrayList<>(); // Split to isolate the part after "VIN : " String postVin = input.split("- VIN : ")[1]; // Split again to isolate the part before " - CODES : " String vinSection = postVin.split(" - CODES : ")[0]; // Split into individual codes String[] codeArray = vinSection.trim().split("\\s+"); for (String code : codeArray) { vinCodes.add(code); } // Output the result System.out.println("Extracted VIN Codes: " + vinCodes); } }
Key Notes:
- Both methods work regardless of what the actual VIN codes are—they only depend on the fixed
VIN :and- CODES :markers. - Using
trim()and splitting on\\s+(one or more spaces) handles any extra spaces that might appear in the input. - If you're using a different language (like Python, C#, etc.), the core logic stays the same: locate the markers, extract the middle section, split into individual elements, and add to a list.
内容的提问来源于stack exchange,提问作者Radek Jackowiak

