简体   繁体   English

如何使用 java 以下面的格式拆分/子字符串为给定的字符串

[英]How to split/substring as given string in below format using java

i want to substring or split into List of Objects of number & description using java or java 8/9我想使用 substring 或 java 或 java 拆分为编号和描述的对象列表 8/9

"9 music recordings; Music files to download. 38 Providing access to music databases and to MP3 websites. 41 Organization of live musical events; Music publishing services; conducting music events; composing music for others; live entertainment production; live performances by musical bands; musical performances; music production; composing music; Operating a music recording studio."

The desired result is期望的结果是

List(0) object: number = 9 ,description = music recordings; Music files to download. 
List(1) object: number = 38 ,description = Providing access to music databases and to MP3 websites.
List(2) object: number = 41 ,description = Organization of live musical events; Music publishing services; conducting music events; composing music for others; live entertainment production; live performances by musical bands; musical performances; music production; composing music; Operating a music recording studio.

You can use the String.split() method with "\\."您可以将String.split()方法与"\\." as your input.作为您的输入。 This will split the whole string into an array of strings.这会将整个字符串拆分为一个字符串数组。 Then you can split on the first space.然后你可以在第一个空间分裂。

Example:例子:

Assuming the object you're trying to create is called A假设您要创建的 object 称为A

class A {
    public A(int num, String description);
}

then you can do:然后你可以这样做:

String str = ...
String[] strings = str.split("\."); // the \\ is so the regex engine doesn't think it's a wildcard character
for (String elem : strings) {
    elements = elem.split(" ", 2); // the 2 is so that it only splits into two strings on the first space
    int number = Integer.valueOf(elements[0]);
    A object = A(number, elements[1]); // do what you want with the object, add it to an array, whatever
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用java split()方法拆分以下字符串? - How to split the below string using java split() method? 如何使用 java 计算给定字符串的 substring 中的总元音? - How to count the total vowels in substring of a given string using java? 使用子字符串作为分隔符拆分或标记Java字符串 - Split or tokenise a java String using a substring as delimiter 使用子字符串(Java)将字符串拆分为字符串数组 - Split a string into an array of strings using substring (Java) 如何拆分java字符串以获得分隔符“,”上的5个子字符串 - How to split java string to get 5 substring on the delimiter "," 如何使用java中的特定规则对给定字符串进行子串 - How to substring a given string with specific rules in java 如何从java字符串中获取子字符串,从下面给定字符串中的\\ n +到\\ n和\\ n-到\\ n的范围开始? - How to fetch substring from java string starting with the range of \n+ to \n and \n- to \n in below given string? 使用Java subString格式化由点(“。”)分隔的字符串 - Using Java subString to format a string separated by dot (“.”) 使用 Java 中的字符串库在给定字符串中 substring 的最大连续重复次数 - Maximum consecutive repeats of a substring in a given string using string library in Java 如何知道给定的字符串是否是 Java 中另一个字符串的子字符串 - How to know if a given string is substring from another string in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM