Java 按多个空格和字符拆分字符串
我有这根绳子 文件名:foto1.jpg 我想把元素放在一个由两个字段组成的字符串中,比如Java 按多个空格和字符拆分字符串,java,Java,我有这根绳子 文件名:foto1.jpg 我想把元素放在一个由两个字段组成的字符串中,比如 {“文件名”,“foto1.jpg”}。我如何在java中做到这一点 我正在努力 split("\\s{2,}:\\s") …但它不起作用。\\s+:\\s+应该适合您: public static void main(String[] args) throws Exception { String s = "File Name : foto1.jpg"; String[] arr =
{“文件名”,“foto1.jpg”}
。我如何在java中做到这一点
我正在努力
split("\\s{2,}:\\s")
…但它不起作用。
\\s+:\\s+
应该适合您:
public static void main(String[] args) throws Exception {
String s = "File Name : foto1.jpg";
String[] arr = s.split("\\s+:\\s+"); // + means one or more
System.out.println(Arrays.toString(arr));
}
O/p:
您可以根据regx将其拆分为:在您的情况下。下面是获得它的示例
public class StringSplit {
public static void main(String[] args) {
String a = "File Name : foto1.jpg";
String[] values = a.split(":", 2);
System.out.println(values[0].trim());
System.out.println(values[1].trim());
}
}
这是合理的,但在
REGEX
中,我们处理每一个细节text
或者在本例中String
代表这项工作,但是如果安装foto1.jpg
我们有foto1
编译器,它不会抛出任何东西,所有东西看起来都不错,但它不是,我们这里没有更多的细节mentioned@Hosseini-输出即使你有foto1
而不是foto1.jpg
这也不会改变,如果图像有foto1.jpg
这样的名称,我们不会重新识别图像,我们有foto1
在我的代码中我们没有提到图像扩展,如果我们没有foto1.jpg
我们添加了错误的扩展,比如foto1.jgp
,谢谢也不起作用,在文件名和之间有多个空格:不,一个空格文件名“多个空格”:“单空格”foto1.jpgI很高兴这对您有帮助@thieba
public class StringSplit {
public static void main(String[] args) {
String a = "File Name : foto1.jpg";
String[] values = a.split(":", 2);
System.out.println(values[0].trim());
System.out.println(values[1].trim());
}
}