There are several answers to similar questions as mine, but I have tried several of them and they are not working. I must be doing something stupid.
I have
String newline = System.getProperty("line.separator");
String content = "Test\n another line\n";
if(content.contains("\\n")) {
content = content.replaceAll("(\\n)", newline);
System.out.print(content);
}
I also tried "\n" and "\\n" in the regex. The content remains unchanged using replaceAll.
Okay facts:
\r is a CR, U+000D
\n is a LF, U+000A
Those characters you can put in a String
String s = "line 1.\nline 2.\n";
String newline = System.getProperty("line.separator");
newline can be "\n" (1 char) or "\r\n" (2 chars) or still something else.
If you would read this text, reading first a backslash and then an n, it would be in code:
String nl = "\\n"; // Two chars, an escaped backslash and a `n`.
String nl = "\\" + 'n'; // Two chars, an escaped backslash and a `n`.
If you would want to replace these two chars with a real newline:
s = s.replace("\\n", "\n");
s = s.replace("\\n", newline); // Platform dependent
Now java regex is still more complex, as it escapes regex letters with a backslash, which in Strings is escaped itself:
You will not need a regex replaceAll/replaceFirst here, but it would go as:
s = s.replaceAll("\\\\n", "\n");
The pattern containing two backslashes: regex escaping of one backslash.
String newline = System.getProperty("line.separator");
String content = "Test\\n another line\\n";
if(content.contains("\\n")) {
content = content.replaceAll("\\\\n", newline);
System.out.print(content);
}
The extra 2 slashes are the escape characters
I also tried this and it works
content.replace("\\n", "\\r\\n")
but
content.replaceAll("\\n", "\\r\\n")
does not.
So in the end I used
while(content.contains("\\n")) {
content = content.replace("\\n", newline);
}
And this solves my problem, not elegant, but it works.
Related
My question is suppose I have a string and a string builder:
String sample = "Ran\"dom";
StringBuilder sb = new StringBuilder("Hello");
On appending the sample string to the StringBuilder and printing the String builder, it returns "HelloRan"dom"
Here the purpose of "\" in sample would be to escape the " character. But, I do not want that action to happen. I want the output of sb after appending sample to be "HelloRan\"dom". How do I do that.
You need to escape the whole "\"" with another "\".
Try,
String sample = "Ran\\\"dom";
You can learn more about escape characters from https://www.freeformatter.com/java-dotnet-escape.html
I have a text file with a mix of newlines (CR/LF or \r\f or "\n") and formfeeds (LF or \f) in a tab \t delimited file. The newlines appear as the expected, "\n" but the formfeeds are also used as internal field delimiters. Example:
COL_1 COL_2 COL_3 COL_4
1 A\fB C\fD 2
Using Java I was able to remove the formfeeds only after I set line.separator to \r - for CR/LF or \r\f and then reading in the file using the FileReader.read() checking for '\n':
private void fixMe() throws Exception{
FileReader in = new FileReader("C:\\somefile.txt");
FileReader out = new FileReader("C:\\someotherfile.txt");
Syetem.setProperty("line.separator","\r");
try {
int c;
while (( c = in.read()) != -1 ) {
if ( c != '\n' ) {
out.write(c);
}
}
}
...
It appears that in.read has a default setting to read "\n" as two characters. I can remove \f but now I'll have to write another method to change \r to a "\n" and reset line.separator as part of the method. Is there a better way to do this? I want to use Scanner, but the solution points at resetting the line.separator setting again which I want to avoid.
Better way to read all file content, then remove "\n and \r\n and \f", after save where you want.
See example:
String content = new String(Files.readAllBytes(Paths.get("path-to-file")));
String processedContent = content.replaceAll("\\n|\\r\\n|\\f", "");
According to your question it seems like you want to skip Line Feed '\f' in the file without skipping if it is CRLF \r\f, so keeping track of last character read might solve your issue.
private void fixMe() throws Exception{
FileReader in = new FileReader("C:\\somefile.txt");
FileReader out = new FileReader("C:\\someotherfile.txt");
//Character 10 'LF' or '\f' and 13 'CR' or '\r'
try {
int c;
int prevCharRead = 0;
while ((c = in.read()) != -1 ) {
if(c==10 && prevCharRead!=13){
//it's a line feed LF '\f' without the occurrence of CR '\r' before it, skip it or implement whatever logic you want.
}else
out.write(c);
prevCharRead = c;
}
}
...
I have problem with BufferedReader.
My source code works fine, but the problem is when I read a value from named pipe it missed some values.
delim="\t";
reader = new BufferedReader(new FileReader("/tmp/base.pip"));
while ((line = reader.readLine())!=null) {
try{
timestamp = Long.parseLong(line.split(delim)[0]);
}
catch(Exception e){
continue;
}
I need to read whole line to get first column value properly.
example
original line : 12345678 A B
readed line: 2345678 A B (missed first bit)
Is there any suggestion to solve this problem?
p.s it works fine, but only a few lines have problem like the above examples.
I've tested your program and it works fine on my computer.
Check your delim String delim = "\t"
Check your file and it has a tab seperator between them.
Check the line value in your program.
If you don't have a tab space, consider using a regular expression which accepts any number of spaces.
String delim = "\\s+";
delim = '\t'
Split cannot take a character as a delimiter. Please check that. It has to be delim = "\t"
Try to split it with Whitspaces and take the first out ouf the array like:
delim = "\\s";
timestamp = Long.parseLong(line.split(delim)[0]);
I think this should solve your problem.
I need to encode a string in such a way that ASCII punctuation character ' will be encoded too. When we don't use gwt it looks like
URLEncoder.encode(string, "UTF-8");
and it works exactly as I expect.
I see this question about URLEncoder equivalent in gwt. But according to the documentation, ASCII punctuation characters
- _ . ! ~ * ' ( )
will not be escaped by method com.google.gwt.http.client.URL.encode(string).
What is the right way to encode a string such that all ' will be encoded too?
Thank you in advance!
If you are using ASCII chars only this would encode all:
String string = "asc<>&-_()'";
String encoded = "";
for(int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
encoded+= "&#x" + Integer.toHexString(Character.valueOf(c)) + ";";
}
The output is:
asc<>&-_()'
I need to split string with delimiter of \n when I use this code:
String delimiter = "\n";
String[] temp;
temp = description2[position].split(delimiter);
for (int i = 0; i < temp.length; i++) {
holder.weeklyparty_text3.setSingleLine(false);
holder.weeklyparty_text3.setText(temp[i]);
}
but not get split string from \n.
You need to escape the backslash in the delimiter string: "\\n"
Split uses regex - so to split on a newline, you should use:
String delimiter = "\\n";
In order to support Unix and Windows new lines use:
String lines[] = String.split("\r?\n");
as described in:
Split Java String by New Line