0

需要分开字符串,我需要分开它。字符串是动态添加的。

例如

1.String a="C:\Wowza Media Systems\Wowza Media Server 2.2.3\content\user2\weight.mp4" 

我需要将它分开user2

2. String a="C:users\Wowza Media Systems\Wowza Media Server 2.2.3\content\user2\sample.flv"

所以我为a动态添加了值,但我需要在content 之后分隔weight.mp4之前的字符串。

4

5 回答 5

1

你也可以接近..

String s="C:/Wowza Media Systems/Wowza Media Server 2.2.3/content/user2/weight.mp4";
String strArray[]=s.split("/"); 
String fileName = strArray[strArray.length-1]; /*weight.mp4*/
int index = s.indexOf(fileName); 
String path = s.substring(0,index) /*C:/Wowza Media Systems/Wowza Media Server 2.2.3/content/user2/*/
于 2012-09-05T11:17:53.503 回答
0

尝试过这样的事情吗?

a = a.replace("users", "");

我很难解释,因为我不太清楚你想做什么。它只是避免“用户”或添加,还是您尝试做更多的事情?

于 2012-09-05T10:39:40.310 回答
0

您只想对两个最后一个斜杠之间的序列进行子串化。查看 String 类的方法 'lastIndexOf' 和 'substring'。

于 2012-09-05T10:47:04.950 回答
0

如果我理解正确,您想从该字符串中获取文件名。如果是这样的话:

如果您的 String a 定义如下:

String a="C:\\Wowza Media Systems\\Wowza Media Server 2.2.3\\content\\user2\\weight.mp4"

试试代码:

String[] split = a.split("\\");
String file = null;
if(split.length!=0) file=split[split.length-1];
System.out.println(file);
于 2012-09-05T10:56:58.180 回答
0
String end = a.substring(a.lastindexof("\\"),a.length); // <- get the end
String tmp = a.substring(0,a.lastindexof("\\"));  // <- get the rest
String start = tmp.substring(0,a.lastindexof("\\"); // <- get the start

我确信上面的代码有一些语法错误,在第一行中你可能必须在 lastindexof 中添加 1。但它为您提供了解决问题的想法。

于 2012-09-05T10:58:13.173 回答