Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
拆分Java字符串_Java_String_Split - Fatal编程技术网

拆分Java字符串

拆分Java字符串,java,string,split,Java,String,Split,标题似乎很简单。但是我没有一个好主意。情况就是这样 我的Java程序中有这样的字符串 String scz="3282E81WHT-22/24"; 我想把上面的字符串分成3个字符串,这样 第一个字符串值应为3282e81 下一个字符串应为WHT(即,上面字符串的字符串部分和此部分始终为3个字符) 下一个字符串值应为22/24(始终出现在-之后) 总之 String first= /* do some expression on scz And value should be "3282e8

标题似乎很简单。但是我没有一个好主意。情况就是这样

我的Java程序中有这样的字符串

String scz="3282E81WHT-22/24";  
我想把上面的字符串分成3个字符串,这样

第一个字符串值应为3282e81

下一个字符串应为WHT(即,上面字符串的字符串部分和
此部分始终为3个字符

下一个字符串值应为22/24
始终出现在-
之后)

总之

String first= /* do some expression on scz And value should be "3282e81" */;
String second= /* do some expression on scz And value should be "WHT" */;
String third= /* do some expression on scz And value should be "22/24" */;
输入也可以是

scz="324P25BLK-12"; 
因此,324P25将是第一个字符串,BLK将是第二个字符串(
,共3个字符)12将是第三个
(在-symbol之后)

如何解决这个问题?

您可以使用subString()方法来实现这个目标。 子字符串具有多个重载

第一串

String first=scz.subString(0,6);
String second=scz.subString(7,9);

您可以使用以下正则表达式取出上述类型字符串:

\d+[A-Z]\d{2}|[A-Z]{3}|(?<=-)[\d/]+

如果scz的字符串长度是固定的,则可以使用

  String first=scz.subString(0,6);
  String second=scz.subString(7,9);
  String third=scz.subString(10,scz.length());

您可以使用这样的正则表达式
(\d+[a-Z]\d+([a-Z]+)-([/\d]+)
,并使用
Matcher.group(int)
方法将字符串拆分为三个组

代码片段

String str = "3282E81WHT-22/24";
//str = "324P25BLK-12";
Pattern pattern = Pattern.compile("(\\d+[A-Z]\\d+)([A-Z]+)-([/\\d]+)");
Matcher match = pattern.matcher(str);
System.out.println(match.matches());
System.out.println(match.group(1));
System.out.println(match.group(2));
System.out.println(match.group(3));
输出

true
3282E81
WHT
22/24

如果字符串的第二部分
(WHT)
等总是由3个字符组成,那么下面的代码肯定会对您有所帮助

String scz = "3282E81WHT-22/24";

            String Third[] = scz.split("-");
            String rev = new StringBuilder(Third[0]).reverse().toString();
            String Second=rev.substring(0,3);
            String First=rev.substring(3,rev.length());

            // here Reverse your String Back to Original
            First=new StringBuilder(First).reverse().toString();
            Second=new StringBuilder(Second).reverse().toString();

            System.out.println(First + "  " + Second + "  " + Third[1]);
然后拆分将按以下顺序进行:)


使用此选项将整个字符串一分为二

String[] parts = issueField.split("-");
String first = parts[0];
String second= parts[1];
使用此选项将第一个字符串拆分为两个

if (first!=null && first.length()>=3){  
   String lastThree=first.substring(first.length()-3);
}

您可以使用字符数组而不是字符串,这样就可以使用数组访问特定字符。 范例

只需指定要使用数组的位置即可访问单独的字符。

您可以尝试此操作

       String scz="3282E81WHT-22/24";
       String[] arr=scz.split("-");
       System.out.println("first: "+arr[0].substring(0,7));
       System.out.println("second: "+arr[0].substring(7,10));
       System.out.println("third: "+arr[1])

查看我的解决方案-

class Main {
    public static void main(String[] args) {
            String first = "";
            String second = "";
            String third = "";

            String scz="3282E81WHT-22/24";
            String[] portions = scz.split("-");
            if (portions.length > 1) {
                    third = portions[1];
            }

            String[] anotherPortions = portions[0].split("[a-zA-Z]+$");
            if (anotherPortions.length > 0) {
                    first = anotherPortions[0];
            }

            second = portions[0].substring(first.length());

            System.out.println(first);
            System.out.println(second);
            System.out.println(third);
    }
}

.

WHT的
零件是否已固定?我的意思是,你想按
WHT
分割还是还会有其他字符?你需要后退一步。为什么第一个元素
3282E81
第二个元素
WHT
和第三个元素
22/24
?。只有当你知道这些规则,你才能实施它们。它会改变的。但那部分将是字符串。它也可以是这样的
scz=324P25BLK-12
324P25将是第一个字符串,BLK将是第二个字符串。12个是third@BevynQ我从一个XML文件中得到这样的输入。在一个标签中。我需要把它分成3个值,就像我在question@Shiju324P25BLK的最后3位数字的长度始终为3?但这是基于长度的。字符串值长度可能会改变,我得到的输出是这样的
18E2823 THW 22/24
前两个是相反的
String[] parts = issueField.split("-");
String first = parts[0];
String second= parts[1];
if (first!=null && first.length()>=3){  
   String lastThree=first.substring(first.length()-3);
}
char scz[] = "3282E81WHT-22/24";
       String scz="3282E81WHT-22/24";
       String[] arr=scz.split("-");
       System.out.println("first: "+arr[0].substring(0,7));
       System.out.println("second: "+arr[0].substring(7,10));
       System.out.println("third: "+arr[1])
class Main {
    public static void main(String[] args) {
            String first = "";
            String second = "";
            String third = "";

            String scz="3282E81WHT-22/24";
            String[] portions = scz.split("-");
            if (portions.length > 1) {
                    third = portions[1];
            }

            String[] anotherPortions = portions[0].split("[a-zA-Z]+$");
            if (anotherPortions.length > 0) {
                    first = anotherPortions[0];
            }

            second = portions[0].substring(first.length());

            System.out.println(first);
            System.out.println(second);
            System.out.println(third);
    }
}