【问题标题】:Working with strings in java : extract a particular string [closed]在java中使用字符串:提取特定字符串[关闭]
【发布时间】:2013-06-24 06:48:48
【问题描述】:

如何在不使用任何索引的情况下获取字符串的特定部分。 我的字符串是这样的:

String path = "D:\SMPPPushGW_SMSCID1_Passive\note.bat";

从上面的字符串中,我想提取

D:\SMPPPushGW_SMSCID1_Passive\

谁能提供代码?

提前致谢。

【问题讨论】:

  • 包括你目前尝试过的代码
  • 不,他们不能。你想要什么?字符串中最后一个非空格分隔的块?第 4 个非空格分隔的块?第一个以 D 开头的块?第一个包含下划线的块?还是 3Ps?
  • 你的代码在哪里???
  • 大家好。。我的要求变了,请再过一遍。 ......

标签: java string


【解决方案1】:

首先将 'path' 设为 StringBuffer 以使其更易于使用。然后使用索引:

StringBuffer path = new StringBuffer("cmd /c start D:\\SMPPPushGW_SMSCID1_Passive\\note.bat");
String result = path.substring(path.indexOf("start")+5, path.lastIndexOf("\\"));

如前所述,文件 API 可能有用,但 URI 也有用。

【讨论】:

    【解决方案2】:

    如果你想要文件的路径,那么你可以使用 java File api's

    String path = "D:\SMPPPushGW_SMSCID1_Passive\note.bat";
    
    File file = new File(path);
    System.out.println(file.getParentFile().getAbsolutePath());
    

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-02
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多