简体   繁体   中英

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.

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

    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:

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_\\\\*")

important thing to remember is that the String you are passing to the split method is really a regular expression. refer to the API for more details: 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. You can use one which may fit in your application.

case 1 : Here is code to split string by a character "." :

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.

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#. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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