To truncate a String here is what I'm using :
要在这里截断一个字符串是我正在使用的:
String test1 = "this is test truncation 1.pdf";
String p1 = test1.substring(0, 10) + "...";
System.out.println(p1);
The output is 'this is te...' How can I access the file name extension so that output becomes : 'this is te... pdf' I could use substring method to access the last three characters but other file extensions could be 4 chars in length such as .aspx
输出是'这是te ...'如何访问文件扩展名,以便输出变为:'this is te ... pdf'我可以使用substring方法访问最后三个字符,但其他文件扩展名可能是长度为4个字符,例如.aspx
Is there a regular expression I can use so that "this is test truncation 1.pdf" becomes "this is te... pdf"
是否有一个正则表达式我可以使用“这是测试截断1.pdf”成为“这是te ... pdf”
7 个解决方案
#1
5
You can do it all with a quick regex replace like this:
您可以使用这样的快速正则表达替换来完成所有操作:
test1.replaceAll("(.{0,10}).*(\\..+)","$1...$2")
#2
2
Do it like this :
像这样做 :
String[] parts = test1.split("\\.");
String ext = parts[parts.length-1];
String p1 = test1.substring(0, 10) + "..."+ext;
System.out.println(p1);
#3
2
You could simply use
你可以简单地使用
test1.substring(0, 10) + "..." + test1.substring(test1.lastIndexOf('.'))
#4
0
For this simple task you don't need Regex
对于这个简单的任务,您不需要正则表达式
String p1 = test1.substring(0, 10) + "..." + test1.substring(test1.lastIndexof('.'));
#5
0
You could use .+[.](.*?)
你可以使用。+ [。](。*?)
This matches all characters including dots, then a dot, then any character, but not greedy, so the first part should grab all of the string preceding the last dot. The capturing group will allow you to retrieve the part you need.
这匹配所有字符,包括点,然后是一个点,然后是任何字符,但不是贪心,所以第一部分应该抓住最后一个点之前的所有字符串。捕获组将允许您检索所需的部件。
#6
0
I use this approach but it will work only if u have single "." and that one too for file name extension.May be this one is a novice approach as u can use split method of String class too.......
我使用这种方法,但只有你有单个“。”才能使用。对于文件扩展名也是如此。可能这是一个新手的方法,因为你可以使用String类的split方法.......
String str="abcdefg ghi.pdf";
int dotIndex=str.indexOf(".")+1;
if(str.indexOf(" ", dotIndex)!=-1)
System.out.println(str.substring(dotIndex,str.indexOf(" ", dotIndex) ));
else
{
System.out.println(str.substring(dotIndex,str.length() ));
}
#7
0
try this -
尝试这个 -
String test1 = "this is test truncation 1.pdf";
String[] test2 = test1.split("\\.");
String p1 = test1.substring(0, 10) + "..." + test2[test2.length-1];
System.out.println(p1);
#1
5
You can do it all with a quick regex replace like this:
您可以使用这样的快速正则表达替换来完成所有操作:
test1.replaceAll("(.{0,10}).*(\\..+)","$1...$2")
#2
2
Do it like this :
像这样做 :
String[] parts = test1.split("\\.");
String ext = parts[parts.length-1];
String p1 = test1.substring(0, 10) + "..."+ext;
System.out.println(p1);
#3
2
You could simply use
你可以简单地使用
test1.substring(0, 10) + "..." + test1.substring(test1.lastIndexOf('.'))
#4
0
For this simple task you don't need Regex
对于这个简单的任务,您不需要正则表达式
String p1 = test1.substring(0, 10) + "..." + test1.substring(test1.lastIndexof('.'));
#5
0
You could use .+[.](.*?)
你可以使用。+ [。](。*?)
This matches all characters including dots, then a dot, then any character, but not greedy, so the first part should grab all of the string preceding the last dot. The capturing group will allow you to retrieve the part you need.
这匹配所有字符,包括点,然后是一个点,然后是任何字符,但不是贪心,所以第一部分应该抓住最后一个点之前的所有字符串。捕获组将允许您检索所需的部件。
#6
0
I use this approach but it will work only if u have single "." and that one too for file name extension.May be this one is a novice approach as u can use split method of String class too.......
我使用这种方法,但只有你有单个“。”才能使用。对于文件扩展名也是如此。可能这是一个新手的方法,因为你可以使用String类的split方法.......
String str="abcdefg ghi.pdf";
int dotIndex=str.indexOf(".")+1;
if(str.indexOf(" ", dotIndex)!=-1)
System.out.println(str.substring(dotIndex,str.indexOf(" ", dotIndex) ));
else
{
System.out.println(str.substring(dotIndex,str.length() ));
}
#7
0
try this -
尝试这个 -
String test1 = "this is test truncation 1.pdf";
String[] test2 = test1.split("\\.");
String p1 = test1.substring(0, 10) + "..." + test2[test2.length-1];
System.out.println(p1);