简体   繁体   中英

Java: Trying to split in “ , but string.split(”“”) is not accepted…how can this be done?

I have some Strings in java, which come from an ontology file and they have the format: <owl:onProperty rdf:resource="http://www.co-ode.org/ontologies/pizza/pizza.owl#WHAT_WE_WANT"/>

I need to make a function that finds if a specific String value is included in WHAT_WE_WANT and then returns the String WHAT_WE_WANT.

So, I splitted every line of the ArrayList at "#", then I searched for the String value into the second part of the splitted lines. Now all I have to do is to get rid of "/>. I was trying to split in " symbol, but I cannot do it, because when I write """ the compiler does not recognize that " is the String symbol I need. Any ideas?

In case my word-explanation was not very good, here is my code:

   if (nextLine.matches(".*" + lookUp + ".*"  ))
    {String lala[]=nextLine.split("#");
    for(int i=0;i<lala.length;i++){
        if(lala[i].matches(".*"+lookUp+".*")){
            String[] temp=lala[i].split( """ ); //<--- doesn't work :/
            System.out.println(temp[0]);
            }
        }
    }

You need to escape the double-quote:

String[] temp = lala[i].split("\"");

I haven't checked whether the rest of your code is actually valid - in particular, I wouldn't use regular expressions just to find a substring unless you're trying to use patterns - but this should solve your immediate issue.

This will give a compilation error:

String[] temp=lala[i].split( """ );

because the string literal is malformed. If you want to split where there is a double-quote character you need to escape the double-quote.

String[] temp=lala[i].split( "\"" );

In a string literal, an unescaped double quote means "this is the end of the string". So the compiler will think that you have written an empty String ( "" ) followed by an unterminated String literal ( " ); . When it reaches the end of the line, the compiler complains that the string is not terminated ... because a Java String literal cannot span multiple lines.


The stuff that you are trying to decode is OWL ... which means you could (and maybe should) parse it using an OWL parser, and RDF parser or an XML parser ... rather than bashing it with regexes, etcetera.

You need to write down String[] temp=lala[i].split( "\\"" );

the backslash is a snignifier for a special character in your String, as a " or a \\ itself.

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