[英]How to parse this string in Java?
prefix/dir1/dir2/dir3/dir4/.. 前缀/ DIR1 / DIR2 / DIR3 / dir4 / ..
How to parse the dir1, dir2
values out of the above string in Java? 如何在Java中解析上述字符串中的dir1, dir2
值?
The prefix here can be: 这里的前缀可以是:
/usr/local/apache2/resumes 在/ usr /本地/的Apache2 /简历
If you want to split the String
at the /
character, the String.split
method will work: 如果要将String
拆分为/
字符, String.split
方法将起作用:
For example: 例如:
String s = "prefix/dir1/dir2/dir3/dir4";
String[] tokens = s.split("/");
for (String t : tokens)
System.out.println(t);
Output 产量
prefix
dir1
dir2
dir3
dir4
Edit 编辑
Case with a /
in the prefix, and we know what the prefix is: 前缀为/
的情况,我们知道前缀是什么:
String s = "slash/prefix/dir1/dir2/dir3/dir4";
String prefix = "slash/prefix/";
String noPrefixStr = s.substring(s.indexOf(prefix) + prefix.length());
String[] tokens = noPrefixStr.split("/");
for (String t : tokens)
System.out.println(t);
The substring without the prefix "slash/prefix/"
is made by the substring
method. 没有前缀"slash/prefix/"
的substring
由substring
方法生成。 That String
is then run through split
. 然后,该String
将通过split
运行。
Output: 输出:
dir1
dir2
dir3
dir4
Edit again 再次编辑
If this String
is actually dealing with file paths, using the File
class is probably more preferable than using string manipulations. 如果此String
实际上处理文件路径,则使用File
类可能比使用字符串操作更可取。 Classes like File
which already take into account all the intricacies of dealing with file paths is going to be more robust. 类似File
类已经考虑了处理文件路径的所有复杂性,这些类将更加健壮。
在这种情况下,为什么不使用new File("prefix/dir1/dir2/dir3/dir4")
并从那里开始?
...
String str = "bla!/bla/bla/"
String parts[] = str.split("/");
//To get fist "bla!"
String dir1 = parts[0];
String result;
String str = "/usr/local/apache2/resumes/dir1/dir2/dir3/dir4";
String regex ="(dir)+[\\d]";
Matcher matcher = Pattern.compile( regex ).matcher( str);
while (matcher.find( ))
{
result = matcher.group();
System.out.println(result);
}
output-- dir1 dir2 dir3 dir4 output-- dir1 dir2 dir3 dir4
Using String.split
method will surely work as told in other answers here. 使用String.split
方法肯定会像在其他答案中所说的那样工作。
Also, StringTokenizer
class can be used to to parse the String using /
as the delimiter. 此外, StringTokenizer
类可用于使用/
作为分隔符来解析String。
import java.util.StringTokenizer;
public class Test
{
public static void main(String []args)
{
String s = "prefix/dir1/dir2/dir3/dir4/..";
StringTokenizer tokenizer = new StringTokenizer(s, "/");
String dir1 = tokenizer.nextToken();
String dir2 = tokenizer.nextToken();
System.out.println("Dir 1 : "+dir1);
System.out.println("Dir 2 : " + dir2);
}
}
Gives the output as : 输出为:
Dir 1 : prefix
Dir 2 : dir1
Here you can find more about StringTokenizer . 在这里您可以找到有关StringTokenizer的更多信息。
String str = "/usr/local/apache/resumes/dir1/dir2"; String prefix = "/usr/local/apache/resumes/"; if( str.startsWith(prefix) ) { str = str.substring(0, prefix.length); String parts[] = str.split("/"); // dir1=parts[0]; // dir2=parts[1]; } else { // It doesn't start with your prefix }
public class Test {
public static void main(String args[]) {
String s = "pre/fix/dir1/dir2/dir3/dir4/..";
String prefix = "pre/fix";
String[] tokens = s.substring(prefix.length()).split("/");
for (int i=0; i<tokens.length; i++) {
System.out.println(tokens[i]);
}
}
}
If it's a File, you can get the parts by creating an instanceof File and then ask for its segments. 如果它是一个文件,您可以通过创建一个instanceof文件然后询问它的段来获取这些部分。
This is good because it'll work regardless of the direction of the slashes; 这很好,因为无论斜线的方向如何,它都能工作; it's platform independent (except for the "drive letters" in windows...) 它是独立于平台的(除了Windows中的“驱动器号”...)
String.split(String regex) is convenient but if you don't need the regular expression handling then go with the substring(..) example, java.util.StringTokenizer or use Apache commons lang 1 . String.split(String regex)很方便,但如果你不需要正则表达式处理,那么请使用substring(..)示例,java.util.StringTokenizer或使用Apache commons lang 1 。 The performance difference when not using regular expressions can be a gain of 1 to 2 orders of magnitude in speed. 不使用正则表达式时的性能差异可以是速度增加1到2个数量级。
String s = "prefix/dir1/dir2/dir3/dir4"
String parts[] = s.split("/");
System.out.println(s[0]); // "prefix"
System.out.println(s[1]); // "dir1"
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.