简体   繁体   中英

Separate String into two parts

My String looks like this: "/namestart blabla1 /nameend blabla2" . PS. it might also be /namestartblabla1/nameendbababa2 for example.

I need to get those into two strings:

string1 == blabla1
string2 == blabla2

I got my strings like this:

if(mystring.startsWith("/namestart")){
    Pattern p = Pattern.compile("/namestart(.*?)/nameend");
    Matcher m = p.matcher(mystring);
    if (m.find()) {
      String string1 = m.group(1);
      String string2 = mystring.substring(mystring.indexOf("/nameend")+8, mystring.length()));
    }
}

First question is can that be done better or is this solution OK?

And second problem is that blabla1 and blabla2 can be anything, it can even be /nameend (probaby wont be) or something that would screw up my reg expression. What would be the theoretical solution for that?

I am not sure what you are trying to do but maybe try using groups indexes instead of substring . To get rid of if(mystring.startsWith("/namestart")){ you can also use ^ anchor at star of your regex to indicate that matching part should also be placed at start of the string.

String mystring = "/namestart blabla1 /nameend blabla2";

Pattern p = Pattern.compile("^(/namestart)(.*?)(/nameend)(.*)");
Matcher m = p.matcher(mystring);
if (m.find()) {
    String string1 = m.group(2);
    String string2 = m.group(4);
    System.out.println(string1 + "|" + string2);
}

output

 blabla1 | blabla2

Also depending on what you are trying to achieve in situation where blabla1 or blabla2 would be /nameend you can try changing (.*?) into (.+?) .

If the format of the string is always as follows,

String Str = new String("/namestart blabla1 /nameend blabla2");

Then the following code might be sufficient,

      for (String temp: Str.split(" ", 4)){
         System.out.println(temp);
      }

you can look at the code below for your solution

public class StackOverflow {
    public static void main(String[] args) {

        String mystring = "/namestart hello /nameend test";

        if(mystring.startsWith("/namestart") && mystring.contains("/nameend")){
        System.out.println("String 1: " + mystring.substring(10, mystring.lastIndexOf("/nameend")).trim());
        System.out.println("String 2: " + mystring.substring(mystring.lastIndexOf("/nameend") + 8).trim());
        }

    }
}

We have to assume that last '/nameend' will be the key for last name as I can see that mystring can be '/namestartblabla1/nameend/nameendbababa2' this also. Here it is difficult to tell that which '/nameend' is the key and which is value. Using the code above you will get

String1 : blabla1/nameend String2 : bababa2

Hope this helps.

The simplest, cleanest approach is to use split() :

String[] parts = mystring.split("\\s*/name(end|start)\\s*");
String string1 = parts[1];
String string2 = parts[2];

The regex passed to split() specifies the separator, which is consumed, so by including optional whitespace before and after the delimiting strings, leading and trailing blanks around the strings are automatically removed.

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