Java 字符串的特定部分
假设我有一个字符串,我如何得到字符串的特定部分Java 字符串的特定部分,java,string,substring,Java,String,Substring,假设我有一个字符串,我如何得到字符串的特定部分 file:/C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/SWA_Playground.ds 我只想得到 C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/ 这个字符串是动态的,所以它并不总是相同的。我只想删除扩展名为.ds的文件单词的开头和最后一部分 我也试了一下 String resourceURI
file:/C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/SWA_Playground.ds
我只想得到
C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/
这个字符串是动态的,所以它并不总是相同的。我只想删除扩展名为.ds的文件单词的开头和最后一部分
我也试了一下
String resourceURI = configModel.eResource().getURI().toString();
//This line gives: file:/C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/SWA_Playground.ds
String sourceFilePath = resourceURI.substring(6, resourceURI.length()-17)
//This line gives C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/
这个resourceURI.length()-17可能会产生问题,因为SWA_playerd.ds并不总是相同的。
我怎样才能从这个字符串中删除最后一部分
谢谢你需要一个正则表达式:
resourceURI.replaceAll("^file:/", "").replaceAll("[^/]*$", "")
你应该使用
首先删除文件:/
前缀,然后有一个windows路径,可以创建一个文件实例
然后使用getParent()
方法获取文件夹路径,使用getName()
获取文件名。如下所示:
String sourceFilePath = resourceURI.substring(
resourceURI.indexOf("/") + 1, resourceURI.lastIndexOf('/') + 1);
基本上,创建一个子字符串,包含第一个斜杠和最后一个斜杠之间的所有内容
输出将是:
C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/
您只是想删除第一个斜杠之前和最后一个斜杠之后的所有内容? 然后这样做:
String resourceURI = configModel.eResource().getURI().toString();
//This line gives: file:/C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/SWA_Playground.ds
String[] sourceFilePathArray = ressourceURI.split("/");
String sourceFilePath = "";
for (int i = 1; i < sourceFilePathArray.length - 1; i++)
sourceFilePath = sourceFilePath + sourceFilePathArray[i] + "/";
//sourceFilePath now equals C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_Playground/
String resourceURI=configModel.eResource().getURI().toString();
//这一行给出:file:/C:/Users/uiqbal/Desktop/IFM_WorkingDirectory/SWA_playerd/SWA_playerd.ds
字符串[]sourceFilePathArray=ressourceURI.split(“/”);
字符串sourceFilePath=“”;
对于(int i=1;i
使用String类中的lastIndexof()方法
String resourceURI = configModel.eResource().getURI().toString();
String sourceFilePath = resourceURI.substring(6, resourceURI.lastIndexOf('.'));
不确定我是否理解您的意思,但是如果您创建了文件的实例,您可以获得它的目录。那是你想要的吗?你可能想检查一下
String resourceURI = configModel.eResource().getURI().toString();
String sourceFilePath = resourceURI.substring(6, resourceURI.lastIndexOf('.'));