Reputation: 49
command = command.replaceAll("\\{player}", e.getPlayer().getName());
What is the purpose of the "\\" in "\\{player}" within that line?
Why doesn't the programmer just do "{player}" ?
Upvotes: 2
Views: 80
Reputation: 25
Backslash are usually to "escape" characters. Characters that usually can be used for regex. In this case, the {} can be interpreted as regex (which you can utilize for string manipulations), hence why you'd have to escape them for your {} to be interpreted as a literal brackets instead of reg ex.
Upvotes: 2
Reputation: 51
"\ with some other characters have special meanings.
For example, \n moves to next line \t writes "tab" character and so on.
Because of this, if you really want to write \ in your code like, "c:\someFolder\someFile"
you should use "C:\\someFolder\\someFile"
\ called escape character
If you write
String text ="firstname\lastname";
compiler gives you an error "illegal escape character" It means that you should not use"\" alone in your code. Happy coding!
Upvotes: 1
Reputation: 311103
That's an escape sequence. Since the {
character has a special meaning in a regex, it needs to be escaped to signify you actually meant the literal {
.
Upvotes: 6