YaoRaoLov 2010-08-14 03:01 采纳率: 50%
浏览 975
已采纳

如何在 Java 中拆分字符串

I have a string, "004-034556", that I want to split into two strings:

string1="004";
string2="034556";

That means the first string will contain the characters before '-', and the second string will contain the characters after '-'. I also want to check if the string has '-' in it. If not, I will throw an exception. How can I do this?

转载于:https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java

  • 写回答

30条回答 默认 最新

  • ℡Wang Yan 2010-08-14 03:05
    关注

    Just use the appropriate method: String#split().

    String string = "004-034556";
    String[] parts = string.split("-");
    String part1 = parts[0]; // 004
    String part2 = parts[1]; // 034556
    

    Note that this takes a regular expression, so remember to escape special characters if necessary.

    there are 12 characters with special meanings: the backslash \, the caret ^, the dollar sign $, the period or dot ., the vertical bar or pipe symbol |, the question mark ?, the asterisk or star *, the plus sign +, the opening parenthesis (, the closing parenthesis ), and the opening square bracket [, the opening curly brace {, These special characters are often called "metacharacters".

    So, if you want to split on e.g. period/dot . which means "any character" in regex, use either backslash \ to escape the individual special character like so split("\\."), or use character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

    String[] parts = string.split(Pattern.quote(".")); // Split on period.
    

    To test beforehand if the string contains certain character(s), just use String#contains().

    if (string.contains("-")) {
        // Split it.
    } else {
        throw new IllegalArgumentException("String " + string + " does not contain -");
    }
    

    Note, this does not take a regular expression. For that, use String#matches() instead.

    If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<= group on the pattern.

    String string = "004-034556";
    String[] parts = string.split("(?<=-)");
    String part1 = parts[0]; // 004-
    String part2 = parts[1]; // 034556
    

    In case you want to have the split character to end up in right hand side, use positive lookahead by prefixing ?= group on the pattern.

    String string = "004-034556";
    String[] parts = string.split("(?=-)");
    String part1 = parts[0]; // 004
    String part2 = parts[1]; // -034556
    

    If you'd like to limit the number of resulting parts, then you can supply the desired number as 2nd argument of split() method.

    String string = "004-034556-42";
    String[] parts = string.split("-", 2);
    String part1 = parts[0]; // 004
    String part2 = parts[1]; // 034556-42
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(29条)

报告相同问题?

悬赏问题

  • ¥15 蓝桥oj3931,请问我错在哪里
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染