String str = "D:*Course 2007 Sem 2*WXET3309*WXET3309_Lab*WXET3309_Lab 2.doc";
		System.out.println(str);
		String[] word = str.split("\\*");
		
		int i = word.length; //length is 1 more than array
		i--; //last index = last string
		System.out.println(word[i]);

The code above is code to get the last string separated by the symbol *. I wonder how should the code be modify if the string is changed to D:\Course 2007 Sem 2\WXET3309\WXET3309_Lab\WXET3309_Lab 2.doc or String str = "D:\\Course 2007 Sem 2\\WXET3309\\WXET3309_Lab\\WXET3309_Lab 2.doc";. Anyone would like to help me to solve this problem?

Dani AI

Generated

Short answer: the compiler error "cannot find symbol variable Pattern" means the class Pattern isn't visible in your file. Add the import for java.util.regex.Pattern or use its fully-qualified name, and 's advice will work. That said, you don't have to use regex at all — for file paths it's often simpler and more robust to use File, Path, or a plain string search.

Add this import so Pattern is recognized:

import java.util.regex.Pattern;

Or try one of these simpler alternatives that avoid regex entirely.

Using java.io.File (works on any Java version and returns the last name component):

import java.io.File;

String last = new File(str).getName();
System.out.println(last);

Using java.nio.file (Java 7+):

import java.nio.file.Paths;

String last = Paths.get(str).getFileName().toString();
System.out.println(last);

Using a plain string search (no imports):

int idx = str.lastIndexOf('\\');
String last = (idx < 0) ? str : str.substring(idx + 1);
System.out.println(last);

Notes and troubleshooting: Java string literals treat backslash as an escape, and regex engines also treat backslash specially — that’s why people use quoting or lots of backslashes when splitting with a regex. If you prefer the split/regex route, importing Pattern fixes the "cannot find symbol" problem. For paths, prefer File/Path because they’re clearer and less error-prone than juggling regex escapes. , adding the import or switching to one of the alternatives above should resolve your compiler error.

Recommended Answers

All 3 Replies

if "\" were the separator, you would do

str.split("\\\\")

the reason is that the argument to split() is compiled into a regular expression; and characters which are special in regex need to be quoted

the backslash is also the escape characters for strings in general, and needs to be doubled

a better way is to use the Pattern.quote() method to quote strings, so you don't have to escape special characters, like

str.split(Pattern.quote("*"));
str.split(Pattern.quote("\\"));

for "*" and "\" separators respectively

thx, it is really a helpful explanation for me to better understand wat is going on actually.

the alternatives way u suggested to me seem not working. My compiler showed me an error message with description "cannot find symbol variable Pattern".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.