Frage

I've inherited some code that I'm trying to modify, and I've see the following syntax in a number pf places.

int titleRecEnd        = inputLine.indexOf("%%headerEnd");  
...
int fileNameStart      = inputLine.indexOf("%%File: ")+8;
int fileNameEnd        = inputLine.indexOf("%%FilenameEnd");

I've reviewed this related link but can;t find an exact match for the dual % either as an answer or in the javadoc associated with the answer. I understand that it has to do with print formattimg, but I can't seem to find syntactical information about %% instead of, %.

What do these veriable declarations do--or what are they supposed to do?

War es hilfreich?

Lösung

%% means % character for java.util.Formatter pattern. Since % denotes the beginning of format specifier %% is used to escape % char.

System.out.println("%%");

prints

%

For String.indexOf % or %% have no special meaning.

Andere Tipps

I'm fairly certain that a double percent is used to create a literal '%', i.e. the first percent sign escapes the second.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax

How you use it there, it appears to simply be part of the string literal. E.g. it might be used to parse a file like this:

%%headerStartSomeHeader%%headerEnd
%%File: image.png%%FilenameEnd

In the context of string formatting, % is a special character used to denote various things; %% resolves to a single literal % symbol.

I am certain it is simply a convention to find pieces of text in a larger string, as a percent normally does not appear double. Some kind of templating. So one can find "keywords" like %%File.

String fileName = inputLine.substring(fileNameStart, fileNameEnd);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top