简体   繁体   中英

How to extract words from a string in Java

I've imported a file and turned it into a String called readFile. The file contains two lines:

qwertyuiop00%
qwertyuiop

I have already extracted the "00" from the string using: String number = readFile.substring(11, 13);

I now want to extract the "ert" and the "uio" in "qwertyuiop" When I try to use the same method as the first, like so:

String e = readFile.substring(16, 19);
String u = readFile.substring(20, 23);

and try to use:

System.out.println(e + "and" + u);

It says string index out of range.

How do I go about this? Is it because the next two words I want to extract from the string are on the second line? If so, how do I extract only the second line? I want to keep it basic, thanks.

UPDATE: it turns out only the first line of the file is being read, does anyone know how to make it so it reads both lines?

If you count the total number of characters for each string, they are more than the indexes your entering.

qwertyuiop00% is 13 characters. Call .length() method on the string to verify the length is the one you expect.

I would debug with adding the following before:

System.out.println(readFile);
System.out.println(readFile.length());

Note:

qwertyuiop00% qwertyuiop is 24 characters since space counts as a character. Unless ofcourse you don't have the space in which it's 23 characters and your indexes are 0 to 22

Note2:

I asked for the parser code since I suspect your using the usual code which is something like:

while ((line = reader.readLine()) != null)

You need to concatenate those lines into one String (though it's not the best approach).

see: How do I create a Java string from the contents of a file?

First split your string into lines, you could do this using

String[] lines = readFile.split("[\r\n]+");

You may want to read the content directly into a List<String> using Files.#readAllLines instead.

second, do not use hard coded indexes, use String#indexOf to find them out. If a substring does not occur in your original string, then the method retunrs -1 , always check for that value and call substring only when the return value is not -1 (0 or greater).

if(lines.length > 1) {
    int startIndex = lines[1].indexOf("ert");
    if(startIndex != -1) {
        // do what you want
    }
}

Btw, there is no point in extracting already known substring from a string

System.out.println(e + "and" + u);

is equivalent to

System.out.println("ertanduio");

Knowing the start and end position of a fixed substring makes only sence if you want to do something with rest of original string, for example removing the substrings.

You may give this a try:-

Scanner sc=new Scanner(new FileReader(new File(The file path for readFile.txt)));
String st="";
while(sc.hasNext()){
st=sc.next();
}
System.out.println(st.substring(2,5)+" "+"and"+" "+st.substring(6,9));

Check out if it works.

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