首次出现字符串中的整数时拆分字符串

时间:2015-01-07 19:57:44

标签: java string

我有一堆这种格式的字符串:

Barry's Restaurant 100 East Main Street

我想将字符串分成两部分:

Barry's Restaurant 
100 East Main Street

所以,我需要在第一次出现整数(街道地址)时拆分字符串。感谢任何帮助,谢谢

3 个答案:

答案 0 :(得分:5)

如果我理解你的问题,你可以用

之类的东西来做
public static void main(String[] args) {
    String line = "Barry's Restaurant 100 East Main Street";
    String[] arr = line.split("\\d+", 2);
    String pt1 = arr[0].trim();
    String pt2 = line.substring(pt1.length() + 1).trim();
    System.out.println(pt1);
    System.out.println(pt2);
}

输出

Barry's Restaurant
100 East Main Street

答案 1 :(得分:2)

为避免重构拆分值,您可以使用实际的正则表达式:

String s = "Barry's Restaurant 100 East Main Street";
Matcher matcher = Pattern.compile("([^\\d]+)(.*)").matcher(s);
if (matcher.matches()) {
    System.out.println(matcher.group(1).trim());
    System.out.println(matcher.group(2));
}

答案 2 :(得分:2)

curl_setopt_array($curl, array(
        CURLOPT_URL => "https://detail.1688.com/offer/535394058500.html?spm=b26110380.sw1688.mof001.302.6YIDM0&v=1",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_POSTFIELDS => "a=b",
        CURLOPT_HTTPHEADER => array(
            "cache-control: no-cache",
            "content-type: application/x-www-form-urlencoded",
            "postman-token: b60107e9-4350-e93f-d6cf-d626505687b8"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response; // return 301 moved permanent
    }