java - 字符串实用程序拆分 - Linux

标签 java string split string-utils

下面的 Java 代码在 Windows 机器上运行

filepath = "euro\football\france\winners.txt";
String[] values = StringUtils.split(filePath, "\\");

if (values != null && values.length >= 4) {

} else {
    //error
}

但是在执行代码时在 linux 中遇到问题。 if 循环没有执行,else 循环正在执行。

对于 linux,我们是否需要将拆分作为“\”或“/”

String[] values = StringUtils.split(filePath, "\\");

任何建议都会有帮助

最佳答案

如果文件在运行 JVM 的机器上,那么您可以使用 File.separatorChar 获取本地机器的系统相关分隔符。

    String[] values = StringUtils.split(filePath, File.separator);

JavaDoc说 (File.separatorChar):

The system-dependent default name-separator character. This field is initialized to contain the first character of the value of the system property file.separator. On UNIX systems the value of this field is '/'; on Microsoft Windows systems it is '\'.

关于java - 字符串实用程序拆分 - Linux,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51399127/

相关文章:

java - 从 url 加载图像

java - 在 Java 中比较整数

java - 如何将首选项与 Activity 联系起来?

c - strlen() 如何计算当前函数中未定义的字符串的长度?

java - 从卡的 toString() 获取错误的字符串

java - 在重复字符之间拆分字符串

java - 更改 apache poi 为 Excel 工作表生成的图表形状

android - 带有字体标签的字符串资源不起作用

PHP 拆分字符串异常

c# - Regex.Split 不保留换行符