如何在反斜杠处拆分java字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23751618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split a java string at backslash
提问by Subbarao Gaddam
String fname="C:\textfiles\db\query\query.txt";
this is the string i need to split it.
这是我需要拆分的字符串。
I tried with this
我试过这个
String [] items=fname.split("\");
But not working.
但不工作。
String [] items=fname.split("\"); also not working...
how to split this string...
如何拆分此字符串...
回答by Keval Trivedi
it works.
有用。
String fname="C:\textfiles\db\query\query.txt";
String split[] = fname.split("\\");
System.out.println(" :: value " + split[0] );
回答by Sanjeev
First of all you can not have a string as you posted in question
首先,您发布的问题中不能有字符串
String fname="C:\textfiles\db\query\query.txt";
this should be replaced by
这应该替换为
String fname="C:\textfiles\db\query\query.txt";
as backslash("\") needs an escape as well.
因为反斜杠(“\”)也需要转义。
Finally you need to do something like this to split them:
最后你需要做这样的事情来拆分它们:
String fname="C:\textfiles\db\query\query.txt";
String[] items= fname.split("\\");
System.out.println(Arrays.toString(items));
Hope this helps.
希望这可以帮助。
回答by Philipp
public static String[] splitPath (String path) {
String backslash = ((char)92) + "";
if (path.contains(backslash)) {
ArrayList<String> parts = new ArrayList<>();
int start = 0;
int end = 0;
for ( int c : path.toCharArray() ) {
if (c == 92) {
parts.add(path.substring(start, end));
start = end + 1;
}
end++;
}
parts.add(path.substring(start));
return parts.toArray( new String[parts.size()] );
}
return path.split("/");
}
回答by Namal Dinesh Ubhayawardana
Use regular expression to escape the character. "String".split("\/");
使用正则表达式来转义字符。"String".split("\/");
回答by Mohit Singh
**//single slash already used as space in java
// suppose it would be Object Type obj="C:\textfiles\db\query\query.txt";**
public static void main(String[] args) {
String target="\";
String replacement="\\";
String str=(String) obj;
str=str.replace(target, replacement);
String[] strarray=str.split(replacement);
String filename=strarray[8];
System.out.println("file Name: "+filename);
}
回答by nandeesh
'split' expects RegEx. Best way to use split is using "Pattern.quote"
'split' 需要 RegEx。使用 split 的最佳方法是使用“Pattern.quote”
String separator = "\";
String value = "C:\Main\text.txt";
String[] arrValues = value.split(Pattern.quote(separator));
回答by Prajwal Nayak
First you need to replace all "\" occurrences with "\\" and then split using "\\".
首先,您需要将所有出现的“\”替换为“\\”,然后使用“\\”进行拆分。
String str="C:\Users\prajwal_nayak\Documents\queries.sql";
String separator = "\";
String[] str_arr=str.replaceAll(Pattern.quote(separator), "\\").split("\\");
回答by Manish Kumar
Please use the following code...
请使用以下代码...
fname.split(Pattern.quote(File.separator));
回答by Anil dhurde
Please use the following code...
请使用以下代码...
String str[]=fname.split("\\\\");