Replace empty string with Non-Disclosed - java

I am a novice in Java so please pardon my inexperience. I have a column (source) like below which has empty strings and I am trying to replace it with Non-Disclosed.
Source
Website
Drive-by
Realtor
Social Media
Billboard
Word of Mouth
Visitor
I tried:
String replacedString = Source.replace("", "Non-Disclosed");
After running the above snippet, everything gets replaced by Non-Disclosed:
Non-Disclosed
Non-Disclosed
Non-Disclosed
............
How can I tackle this issue? Any assistance would be appreciated.

I think you simply have to do : Source.replace("\n\n", "\nNon-Disclosed\n")

I am assuming that your entire column is stored in one string.
In that case you can use ^$ regex to represent empty line (with MULTILINE flag (?m) which will allow ^ and $ to represent start and end of lines).
This approach
will work for many line separators \r \n \r\n
will not consume those line separators so we don't need to add them back in replacement part.
To use regex while replacing we can use replaceAll(regex, replacement) method
DEMO:
String text = "Source\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
"Website\r\n" +
"Drive-by\r\n" +
"Realtor\r\n" +
"Social Media\r\n" +
"\r\n" +
"Billboard\r\n" +
"\r\n" +
"Word of Mouth\r\n" +
"\r\n" +
"Visitor";
text = text.replaceAll("(?m)^$", "Non-Disclosed");
System.out.println(text);
Output:
Source
Non-Disclosed
Non-Disclosed
Non-Disclosed
Website
Drive-by
Realtor
Social Media
Non-Disclosed
Billboard
Non-Disclosed
Word of Mouth
Non-Disclosed
Visitor

You can use
String replacedString = Source.trim().isEmpty() ? "Non-Disclosed" : Source;
to replace only the "blank" String.

Related

Java remove unwanted spaces in a text file and replace with character

I have the following text file. I want to remove the lines and spaces so that the text file has a clear delimter to process. I cannot think of any way to remove the gaps between lines, is there a way?
Student+James Smith+Status: Current Student+Student+James Fits+Status: Not a current Student
Textfile
Student
James Smith
Status: Current Student
Student
James Fits
Status: Not a current Student
I know that this
a.replaceAll("\\s+","");
removes whitespaces.
You could remove end of line characters in a similar fashion
a.replaceAll("\n","");
Where 'a' is a String.
use a regex take the whole text in to a string and
string txt = "whole String";
String formatted = txt.replaceAll("[^A-Za-z0-9]", "-");
this will result in changing + sign and " " to replace with "-" sign. so now you have a specific deleimeter.
Something like find \s*\r?\n\s* replace +
Trims whitespace and adds delimiter '+'
Result:
Student+James Smith+Status: Current Student+Student+James Fits+Status: Not a current Student
Try using this one.
\n+\s*
just use it like this :
yourStrVar.replaceAll("\n+ *", "+")

Apache POI Anomalous Whitespace (Resolved: \u00A0 non-breaking space)

EDIT: Resolved Answer: Was a 00a0 nonbreaking space, not a c0a0 nonbreaking space.
After using Apache POI to convert from docx to plaintext, and then reading the plaintext into Java and trying to parse it I've run into the following problems.
Output:
" "
first characterequals SPACE OR TAB
false
[B#5e481248
[B#66d3c617
ARRAYTOSTRING SPACE: [32]
ARRAYTOSTRING ?????: [-62, -96]
For code:
System.out.println("\t\"" + line.substring(0,1) + "\"\n\tfirst characterequals SPACE OR TAB \n\t" + (line.substring(0,1).equals(" ")
|| line.substring(0,1).equals("\t") ));
System.out.println(line.substring(0,1).getBytes());
System.out.println(" ".getBytes());
System.out.println("ARRAYTOSTRING SPACE: " + Arrays.toString(" ".getBytes()));
System.out.println("ARRAYTOSTRING ?????: " + Arrays.toString(line.substring(0,1).getBytes()));
String.trim() does not get rid of it
String.replaceAll("\s" , "") does not get rid of it
I'm trying to parse an enormous materials document and this is turning into a major hurdle. I have no idea what's going on or how to interface with it, can anyone shed some light on what's going on here?
This translates to the bytes with hex codes c2 a0, which according to this answer is a UTF-8 encoded non-breaking space. Note that this is not really a space and \s will not match it.
this worked for me:
String valor = org.apache.commons.lang3.StringUtils.normalizeSpace(java.text.Normalizer.normalize(valor, java.text.Normalizer.Form.NFD));

Line separator for Windows in java

I would like to save some words into *.txt file. But there is one condition: each word must be on new line. So I wrote this:
String content ="";
String nl = System.lineSeparator();
content += "my" + nl + "some" + nl + "random" + nl + "words";
But this code not working - all words are in the same line.
So i tried with special characters - \r\n :
String content ="";
content += "my" + "\r\n" + "some" + "\r\n" + "random" + "\r\n" + "words";
Still didn't works - in file all words are in same line:
mysomerandomwords
In addition to this: my string content is save to file by:
<button class="button">Download</button>
What kind of separator should I use to put words in another lines?
(I'm using Netbeans 8.0. File is opens in windows notepad).
Use base64 in data URI scheme:
<button class="button">Download</button>
In your Java code do:
String content ="";
String nl = System.getProperty("line.separator");
content += "my" + nl + "some" + nl + "random" + nl + "words";
content = DatatypeConverter.printBase64Binary(content.getBytes("utf-8"));
Demo: http://jsfiddle.net/bDRAq
Try with System.getProperty("line.separator")
Read it here about System Properties with complete list of System properties.
The System class maintains a Properties object that describes the configuration of the current working environment.
"line.separator" - Sequence used by operating system to separate lines in text files
What System.lineSeparator() states?
Returns the system-dependent line separator string. It always returns the same value - the initial value of the system property line.separator.
On UNIX systems, it returns "\n"; on Microsoft Windows systems it returns "\r\n".
Why use System.getProperty()?
"line.separator" property can be changed by passing arguments as shown below
java -Dline.separator=
Have a look at Should I cache System.getProperty(“line.separator”)?
Well, it depends how you write down the content. If you write it to a file on the filesystem, it will most likely end up correctly. But you are not saving it to a file with your program, but you let the browser do that by including a link in a html file. When including characters in HTML, you need to encode them with the URLEncoder class.
Since it looks to me that you are working within an WebContainer, since you JSP structures, you could also implement a Servlet that returns the requested data.

How to print text on new line using "\n"

I try to create one big String in appropriate format as i want and print it using PrinterJob class. Here is the code of String:
String bigtext = "The name\n" + "some another text";
Graphics2D's_object.drawString(bigtext, 50, 50);
But it prints as "The name some another text" in one line, "\n" does not work, while i want to print "some another text" in another line.
P.S. I try to print bigtext in printer.
SOLVED: Here is the solution: Problems with newline in Graphics2D.drawString. (after long trouble :))
The linefeed character \n is not the line separator in certain operating systems use \r\n. Additionally i would recommend use of StringBuilder rather then using +
Edit : You can use System.getProperty("line.separator"); as well.
A similar question was asked yesterday and this is what helped:
The problem that you are encountering is because of the "line separator" you are hard coding. It's best to get the System's line separator with:
System.getProperty("line.separator");
So that your code would look like this:
String lineseparator=System.getProperty("line.separator");
// I'd suggest putting this as a class variable, so that it only gets called once
// rather than everytime you call the addLine() method
String bigtext = "The name" + lineseparator + "some another text";
//If you wanted an empty line in between them, then add the lineseparator twice
String bigtext = "The name" + lineseparator + lineseparator + "some another text";
It seems most of the guys don't understand the question. I tried the PrinterJob and it doesn't work for me neither.
I found a solution but not verifyied:
How to print strings with line breaks in java

Representing carriage returns and quotes with regular expressions in java

I want to replace a carriage return followed by quotation marks with just quotation marks. For example, if I have:
Hello World
"Hello World"
I would like the result to be:
Hello World"Hello World"
This is my attempt, where String text is what I have above:
String adjusted = text.replaceAll("[\n][\"], "\"");
However, my IDE does not accept this.
Thanks for the help!
String adjusted = text.replaceAll("(?m)\r?\n\"", "\"");
The (?m) is for multi-line usage, for \r for a real CR in Windows (CR+LF).
You can use replace instead of replaceAll to avoid matching regular expression, but instead matching literals.
String adjusted = text.replace("\n\"", "\"");
If you want this method to use you operating system line separators you should use
String adjusted = text.replace(System.lineSeparator()+"\"", "\"");
You should do it in a platform agnostic way like:
String newline = System.getProperty("line.separator");
String newStr = str.replaceAll(newline, "\"");

Categories

Resources