简体   繁体   English

如何在Java中分割'*'字符串

[英]How to split a '*' String in Java

i have problem to split string with ' split_' , it seem my java netbean cant split when ' split_' is used. 我有问题拆分字符串“split_”,似乎我的Java netbean着分裂时,“split_”被使用。

any idea how we can overcame this? 知道我们如何克服这个问题吗?

i refer to this solution but it can only split without the used of '*'. 我指的是这种解决方案,但它只能在不使用'*'的情况下拆分。 How to split a string in Java 如何在Java中分割字符串

    String echoPHP= "test*split_*test2";

    String[] strArray = echoPHP.split("*split_*");

    String part1 = strArray2[0]; // 004
    String part2 = strArray2[1]; // 034556



    System.out.println(strArray[0]);
    System.out.println(strArray[1]);

error is: 错误是:

    Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*split_*

output supposed to be: 输出应该是:

test
test2

Use Pattern.quote() around your split string to ensure it's taken as a literal, not a regular expression: 在拆分字符串周围使用Pattern.quote() ,以确保将其视为文字而不是正则表达式:

String[] strArray = echoPHP.split(Pattern.quote("*split_*"));

You'll have difficulties otherwise, since * is a special character in regular expressions used to match any number of occurrences of the character or group that proceeded it. 否则您会遇到困难,因为*是正则表达式中的特殊字符,用于匹配出现在该字符或字符组后面的任意数量的字符。

Of course, you could manually escape all the special characters used in regular expressions using \\ , but this is both less clear and more error prone if you don't want to use any regular expression features. 当然,您可以使用\\手动转义正则表达式中使用的所有特殊字符,但是如果您不希望使用任何正则表达式功能,则这既不太清晰,也更容易出错。

try: echoPHP.split("\\\\*split_\\\\*") 尝试: echoPHP.split("\\\\*split_\\\\*")

important thing to remember is that the String you are passing to the split method is really a regular expression. 要记住的重要一点是,传递给split方法的String实际上是一个正则表达式。 refer to the API for more details: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) 有关更多详细信息,请参考API: http : //docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

Here are different cases to split string in java. 这是在Java中拆分字符串的不同情况。 You can use one which may fit in your application. 您可以使用一种适合您的应用程序。

case 1 : Here is code to split string by a character "." 情况1:这是用字符“。”分割字符串的代码。 :

String imageName = "picture1.jpg";
String [] imageNameArray = imageName.split("\\.");
for(int i =0; i< imageNameArray.length ; i++)
{
   system.out.println(imageNameArray[i]);
}

And what if mistakenly there are spaces left before or after "." 如果错误地在“。”之前或之后还留有空格,该怎么办? in such cases? 在这种情况下? It's always best practice to consider those spaces also. 最好也考虑这些空间。

String imageName = "picture1  . jpg";
    String [] imageNameArray = imageName.split("\\s*.\\s*");
        for(int i =0; i< imageNameArray.length ; i++)
        {
           system.out.println(imageNameArray[i]);
        }

Here, \\\\s* is there to consider the spaces and give you only required splitted strings. 在这里,\\\\ s *可以考虑空格,并且只为您提供所需的分割字符串。

Now, suppose you have placed parameters in between two special charaters like : #parameter# or parameter or even two differnt signs at a time like *paramter#. 现在,假设您已将参数放置在两个特殊字符之间,例如:#parameter#或parameter或甚至两个不同的符号之间,例如* paramter#。 We can have list of all these parameters between those signs by this code : 我们可以通过以下代码在这些符号之间列出所有这些参数:

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;

public class Splitter {

    public static void main(String[] args) {

        String pattern1 = "#";
        String pattern2 = "#";
        String text = "(#n1_1#/#n2_2#)*2/#n1_1#*34/#n4_4#";

        Pattern p = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2));
        Matcher m = p.matcher(text);
        while (m.find()) {
            ArrayList parameters = new ArrayList<>();
            parameters.add(m.group(1));
            System.out.println(parameters);
            ArrayList result = new ArrayList<>();
            result.add(parameters);
            // System.out.println(result.size());
        }

    }
}

Here list result will have parameters n1_1,n2_2,n4_4. 此处的列表结果将具有参数n1_1,n2_2,n4_4。

You can use split method like this 您可以使用像这样的分割方法

 String[] strArray = echoPHP.split("\\*split_\\*");
  • character is the special charater.. so you should use "\\" in front of * character. 字符是特殊字符。.因此,应在*字符前使用“ \\”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM