在正斜杠上拆分字符串 [英] Split the string on forward slash

查看:136
本文介绍了在正斜杠上拆分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码,我想根据正斜杠拆分/\".

I have a code which I wanted to split based on the forward slash "/".

每当我根据////进行正则表达式拆分时它永远不会分裂并给我整个字符串。
我尝试用文件分隔符代替\,然后用\\分割,但不是下面的代码。

Whenever I have a regex split based on "////" it never splits and gives me the whole string back. I tried replacing with file separator which gives "\" and then splitting with "\\" works but not the below code.

下面是测试的代码

package org.saurav.simpletests.string;

import java.io.File;

public class StringManipulator {

    public static void main(String a[]){
        String testString ="/UserId/XCode/deep";

        //testString = testString.replace("/", File.separator);
        //testString = testString.replace("/", "_");
        testSplitStrings(testString);
    }

    /**
     * Test the split string
     * @param path
     */
    public static void testSplitStrings(String path){
        System.out.println("splitting of sprint starts \n");
        String[] paths = path.split("////");
        for (int i = 0; i < paths.length; i++) {
            System.out.println("paths::"+i+" "+paths[i]+"\n");
        }
        System.out.println("splitting of sprint ends");
    }
}

欢呼,
Saurav

cheers, Saurav

推荐答案

无需转义正斜杠。如果您这样做,您的代码就可以正常工作:

There is no need to escape forward slashes. Your code works fine if you just do:

String[] paths = path.split("/");

这篇关于在正斜杠上拆分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆