简体   繁体   English

Java,子字符串从字符串获取值

[英]Java, Substring to get value from string

given a string like this 给定这样的字符串

string a = "Course Name:\n" + CourseName
                         +"\n\nVenue:\n" + locationName
                         +"\n\nStart Time:\n" + startTime 
                         +"\n\nEnd Time:\n"+ endTime";

CourseName , locationName , startTime and endTime would be a variable and being assigned to string a, how can i get all these value from the string using substring or regex and store all of it into different variable? CourseNamelocationNamestartTimeendTime将是一个变量并分配给string a,我如何使用substring或正则表达式从字符串中获取所有这些值并将其存储到不同的变量中? please note that I don't have access all these variable, I can only access the string which leave me the only option is play around with substring or regex . 请注意,我没有访问所有这些变量,我只能访问字符串,这使我唯一的选择是使用substringregex

You could split by newline 你可以换行

String[] parts = a.split("\\n");
String courseName = parts[1];
String locationName = parts[4];
String startTime= parts[7];
String endTime= parts[10];

or you can use: 或者您可以使用:

StringTokenizer tokens=new StringTokenizer(a, "\n");

while(tokens.hasMoreTokens()){
            System.out.println(tokens.nextToken());
        }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM