[英]Trim from a space or tab onward or backwards in Java
I know how to trim things in Java but how does one trim from a space or tab to the left or to the right? 我知道如何用Java修剪内容,但是如何从空格或制表符向左边或右边修剪呢? I have a program that searches for some 4 character prefixes let's say this prefix is
XYBC
then it has X amount of characters to it like XYBC4975723434
but the line that my code takes looks like this: 我有一个搜索4个字符前缀的程序,假设这个前缀是
XYBC
然后它就有X个字符,例如XYBC4975723434
但是我的代码XYBC4975723434
的行像这样:
Viuhaskfdksjfkds XYBC4975723434 fkdsjkfjaksjfklsdakldjsen
But then I would like it to trim it to this: XYBC4975723434
Thanks 但是,然后我希望将其调整为:
XYBC4975723434
谢谢
That is not a trim, but a regular expression find, using the following regex: 这不是修剪,而是使用以下正则表达式查找正则表达式:
\bXYBC.*?\b
That expression used word boundaries, which may not be what you want. 该表达式使用了单词边界,这可能不是您想要的。
For whitespace, use: 对于空格,请使用:
(?<=^|\s)XYBC\S*
Test 测试
public static void main(String[] args) {
test("Viuhaskfdksjfkds XYBC4975723434 fkdsjkfjaksjfklsdakldjsen");
test("XYBC4975723434");
test("Viuhaskfdksjfkds xXYBC4975723434 fkdsjkfjaksjfklsdakldjsen");
test("abc XYBC49-75(723)4$34 xyz");
}
private static void test(String text) {
Matcher m = Pattern.compile("\\bXYBC.*?\\b").matcher(text);
if (m.find()) {
System.out.println(m.group());
} else {
System.out.println("Not found: " + text);
}
}
Output (word boundary) 输出(字边界)
XYBC4975723434
XYBC4975723434
Not found: Viuhaskfdksjfkds xXYBC4975723434 fkdsjkfjaksjfklsdakldjsen
XYBC49
Output (whitespace) 输出(空格)
XYBC4975723434
XYBC4975723434
Not found: Viuhaskfdksjfkds xXYBC4975723434 fkdsjkfjaksjfklsdakldjsen
XYBC49-75(723)4$34
I have a prog that searches for some 4 character prefixes let's say this prefix is XYBC then it has X amount of characters to it
我有一个编,它搜索一些4个字符的前缀,假设此前缀为XYBC,则它具有X个字符
If you want your program to grab a substring with specific starting letters and grab a specific number of letters, you may do this (apart from using regular expression): 如果您希望程序抓取具有特定起始字母的子字符串并抓取特定数量的字母,则可以执行此操作(除了使用正则表达式):
public static String grabText(String str, String find, int n){
int idx = str.indexOf(find);
if(idx == -1)
return "";
else{
String sub = str.substring(idx, Math.min(n, str.length() - idx)+ idx);
return sub;
}
}
TEST: 测试:
String str = "Viuhaskfdksjfkds XYBC4975723434 fkdsjkfjaksjfklsdakldjsen";
System.out.println(grabText(str, "XYBC", 14));
System.out.println(grabText(str, "XYBC", 5));
System.out.println(grabText(str, "XYBC", 100));
System.out.println(grabText(str, "XYBC", 9999));
OUTPUT: OUTPUT:
XYBC4975723434
XYBC4
XYBC4975723434 fkdsjkfjaksjfklsdakldjsen
XYBC4975723434 fkdsjkfjaksjfklsdakldjsen
You can use regular expressions to achieve this. 您可以使用正则表达式来实现。 For example, by using the regular expression
XYBC\\d+
, you can match or find any sequence of numbers which are prefixed with XYBC
. 例如,通过使用正则表达式
XYBC\\d+
,您可以匹配或找到任何以XYBC
为前缀的数字序列。 In code, that would look like this: 在代码中,如下所示:
String input = "Viuhaskfdksjfkds XYBC4975723434 fkdsjkfjaksjfklsdakldjsen";
Pattern pattern = Pattern.compile("XYBC\\d+");
Matcher matcher = pattern.matcher(input);
if (!matcher.find()) throw new RuntimeException("failed to find any numbers prefixed with XYBC!");
System.out.println(matcher.group()); // prints "XYBC4975723434"
Also check out the details of the given regular expression here . 还可以在此处检查给定正则表达式的详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.