sau*_*rav 23 java string split
我有一个代码,我想根据正斜杠"/"拆分.
每当我有一个基于"////"的正则表达式分割时,它永远不会分裂并给我整个字符串.我尝试用文件分隔符替换,它给出了"\",然后用"\\"分割,但不是下面的代码.
下面是测试的代码
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");
}
}
Run Code Online (Sandbox Code Playgroud)
欢呼,索拉夫
Kep*_*pil 44
没有必要逃避正斜杠.如果你这样做,你的代码工作正常:
String[] paths = path.split("/");
Run Code Online (Sandbox Code Playgroud)
小智 7
我想以格式检查输入日期的验证,dd/mm/yyyy因此需要拆分我的字符串/您可以简单地通过以下方式完成:
String spl[]=str.split("/");
int date=Integer.parseInt(spl[0]);
int month=Integer.parseInt(spl[1]);
int year=Integer.parseInt(spl[2]);
Run Code Online (Sandbox Code Playgroud)