How to use line break argument
I wrote the most simple Hello, World! application:
public class Helloworld {
public static void main(String[] args) {
System.out.println("Hello\nHello");
}
}
When it runs, the result is:
Hello
Hello
But if I use Hello\nHello as arguments,
public class Helloworld {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
the result is Hello\nHello. How do I get the two-line result?
When you define a String as "Hello\nHello" in Java, it contains no '\' character. It is an escape sequence for the line break: "\n" is just one character.
When you use this string as an argument to your program, however (so the string is defined outside), "\n" is interpreted as two characters: '\' and 'n'. You have to replace these two characters with the line break, knowing that to match '\' you have to escape it with '\':
System.out.println(args[0].replace("\\n", "\n"));
For portability concerns, you can also use System.lineSeparator() as the replacement string.
If you are using a Unix-like OS such as GNU/Linux or MacOS, or a Bash shell on any other system (such as Cygwin on Windows), then just enclose your command-line argument in single quotes, and you can insert any number of newlines and it will still be treated as a single argument:
$ java Helloworld 'line one
line two' <-- this is a single argument with an embedded newline
line one <-- it prints out the output on separate lines!
line two
This will not work on the default Windows Command Processor (cmd.exe). If that case, you might want to use the following technique.
You could use the StringEscapeUtils.unescapeJava method from Apache Commons Text. Then you will be able to pass command-line arguments and have them interpreted exactly[*] like a literal string in source code:
import static org.apache.commons.text.StringEscapeUtils.unescapeJava;
public class Helloworld {
public static void main(String[] args) {
System.out.println(unescapeJava(args[0]));
}
}
[*] Barring any remaining bugs in the Apache method.
Apparently you can't do it straight away but doing the following trick will get you what you need:
System.out.println(String.format(args[0]));
Here String#format is used and the new line is passed as the conversion sequence for a new line. (see the format string syntax).
So call your program with
java Helloworld "hello%nnworld"
will print
hello
workd
and if you need to output %n itself then you can quote '%' with another '%' i.e.
java Helloworld "hello%%nnworld"
The above will print:
hello%nnworld
public class Helloworld {
public static void main(String[] args) {
System.out.println("Hello\nHello");
}
}
When it runs, the result is:
Hello
Hello
But if I use Hello\nHello as arguments,
public class Helloworld {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
the result is Hello\nHello. How do I get the two-line result?
When you define a String as "Hello\nHello" in Java, it contains no '\' character. It is an escape sequence for the line break: "\n" is just one character.
When you use this string as an argument to your program, however (so the string is defined outside), "\n" is interpreted as two characters: '\' and 'n'. You have to replace these two characters with the line break, knowing that to match '\' you have to escape it with '\':
System.out.println(args[0].replace("\\n", "\n"));
For portability concerns, you can also use System.lineSeparator() as the replacement string.
If you are using a Unix-like OS such as GNU/Linux or MacOS, or a Bash shell on any other system (such as Cygwin on Windows), then just enclose your command-line argument in single quotes, and you can insert any number of newlines and it will still be treated as a single argument:
$ java Helloworld 'line one
line two' <-- this is a single argument with an embedded newline
line one <-- it prints out the output on separate lines!
line two
This will not work on the default Windows Command Processor (cmd.exe). If that case, you might want to use the following technique.
You could use the StringEscapeUtils.unescapeJava method from Apache Commons Text. Then you will be able to pass command-line arguments and have them interpreted exactly[*] like a literal string in source code:
import static org.apache.commons.text.StringEscapeUtils.unescapeJava;
public class Helloworld {
public static void main(String[] args) {
System.out.println(unescapeJava(args[0]));
}
}
[*] Barring any remaining bugs in the Apache method.
Apparently you can't do it straight away but doing the following trick will get you what you need:
System.out.println(String.format(args[0]));
Here String#format is used and the new line is passed as the conversion sequence for a new line. (see the format string syntax).
So call your program with
java Helloworld "hello%nnworld"
will print
hello
workd
and if you need to output %n itself then you can quote '%' with another '%' i.e.
java Helloworld "hello%%nnworld"
The above will print:
hello%nnworld
Comments
Post a Comment