Im importing Excel data to Java program. Having a column that should eliminate whitespace in case user mistype on the column.
Example value : "12341 "
i've used
replaceAll("\\s+", "");
replaceAll(" ", "");
StringUtils.trim(stringValue);
However, it still return "12341 " with length :6. It didn't remove the unnecessary white-spaces
EDIT
Complete code for replace return.
stringArray[x] = stringArray[x].replaceAll("\\s+", "");
stringArray[x] = stringArray[x].replaceAll(" ", "");
stringArray[x] = StringUtils.trim(stringArray[x]);
This should work:
stringValue = stringValue.replaceAll(" ", "");
You need to use the returned value.
I too face the same problem and i resolved it by using
text = text.replaceAll("[^\x00-\x7F]", "");
And make sure this will remove your special character too
Ref link :
https://howtodoinjava.com/regex/java-clean-ascii-text-non-printable-chars/
Related
I am trying to open new email from my Java app:
String str=String.valueOf(email);
String body="This is body";
String subject="Hello worlds";
String newStr="mailto:"+str.trim()+"?subject="+URLEncoder.encode(subject,"UTF-8")+"&body="+URLEncoder.encode(body, "UTF-8")+"";
Desktop.getDesktop().mail(new URI(newStr));
Here it is my URLEncoding. As I cannot use body or subject string in URL without encoding them, my output here is with "+" instead of whitespace. Which is normal, I understand that. I was thinking if there is a way to visualize subject and body normally in my message? I tried with .replace("+"," ") but it is not working as it is giving an error. This is how it is now:
I think there might be different character set but I am not sure.
That's the way URLEncoder works.
One possible approach would be to replace all + with %20 after URLEncoder.enocde(...)
Or you could rely on URI constructor to encode your parameters correctly:
String scheme = "mailto";
String recipient = "recipient#snakeoil.com";
String subject = "The Meaning of Life";
String content = "..., the universe and all the rest is 42.\n Rly? Just kidding. Special characters: äöü";
String path = "";
String query = "subject=" + subject + "&body=" + content;
Desktop.getDesktop().mail(new URI(scheme, recipient, path, query, null));
Both solutions have issues:
In the first approach, you might replace actual + signs, with the second, you'll have issues with & character.
I'm writing a text to HTML converter.
I'm looking for a simple way to wrap each line of text (which ends with carriage return) with
<p>.....text.....</p>
Can you suggest some String replacement/regular expression that will work in Java ?
Thanks
String txtFileContent = ....;
String htmlContent = "<p>" + txtFileContent.replaceAll("\\n","</p>\\n<p>") + "</p>";
Assuming,
line delimitter is "\n".
One line is one paragraph.
The end of txtFileContent is not "\n"
Hope this help
Try using StringEscapeUtils.escapeHtml and then adding the tags you want at the beginning end.
String escapeHTML = StringEscapeUtils.escapeHtml(inputStr);
String output = "<p>"+escapeHTML+"</p>";
I am struggling to find the best solution. Below is my XML :
<Dbtr>
<Nm>John doe</Nm>
<Id>
<OrgId>
<Othr>
<Id/>
</Othr>
</OrgId>
</Id>
</Dbtr>
This is should replaced like this below :
<Dbtr>
<Nm>John doe</Nm>
</Dbtr>
So all the empty nodes and children without any values should be left out.
I am using following expression and it don't work as per my wishes
docStr = docStr.replaceAll("<(\\w+)></\\1>|<\\w+/>", "");
Any help would be really appreciated.
Edit :
I am creating this XML (and not parsing it) this will be sent out to clearing house, who will reject this xml message because of this empty tags. The way I am creating this xml is not in my hand I just provide the values from the db and as you can see some of the values are empty, this code (I have no control) writes out the xml tag already and then writes the value, all I can control is to not write "null".
The best bet for me now is to get the output xml like this and replace it with some regexp logic and form an xml without empty tags, that can pass schema validation.
String xml = ""
+ "<Dbtr>"
+ " <Nm>John doe</Nm>"
+ " <Id>"
+ " <OrgId>"
+ " <Othr>"
+ " <Id/>"
+ " </Othr>"
+ " </OrgId>"
+ " </Id>"
+ "</Dbtr>";
while (true) {
String repl = xml.replaceAll("<(\\w+)>\\s*</\\1>|<\\w+/>", "");
if (repl.length() == xml.length())
break;
xml = repl;
}
System.out.println(xml);
// -> <Dbtr> <Nm>John doe</Nm> </Dbtr>
i use this simple code to rename a file when an event happens:
String newFileName = oldFileName + "_" + new Date().getTime();
if the event happens more and more time i will have a string like:
myfile_1372933712717_1372933715279_1372933716234
while i would like to have only the last timestamp...
Of course i could do a substring to remove the string after "_" and replace it with the new timestamp, but let's suppose i will have a file like: myfile_mycomment...mycomment will be replaced and it's not a good thing...
So how could i recognize if there is already a filestamp in the name of the file?!?
You can try to approach this with RegEx, as the timestamps will always have the same pattern. By this, you can differ between comments and timestamps and remove only the timestamps.
This code
String test = "Hallo_Comment_1372933712717_1372933712717";
test = test.replaceAll("_1[0-9_]{12}", "");
System.out.println(test);
generated this output
Hallo_Comment
Assuming your original file name does'nt contains "_"
Before appending split file name with "_" and get always the 0th element from the string array and append the timestamp
I have a requirement in my project.
I generate a comment string in javascript.
Coping Option: Delete all codes and replace
Source Proj Num: R21AR058864-02
Source PI Last Name: SZALAI
Appl ID: 7924675; File Year: 7924675
I send this to server where I store it as a string in db and then after that I retrieve it back and show it in a textarea.
I generate it in javascript as :
codingHistoryComment += 'Source Proj Num: <%=mDefault.getFullProjectNumber()%>'+'\n';
codingHistoryComment += 'Source PI Last Name: <%=mDefault.getPILastName()%>'+'\n';
codingHistoryComment += 'Appl ID: <%=mDefault.getApplId()%>; File Year: <%=mDefault.getApplId()%>'+'\n';
In java I am trying to replace the \n to :
String str = soChild2.getChild("codingHistoryComment").getValue().trim();
if(str.contains("\\n")){
str = str.replaceAll("(\r\n|\n)", "<br>");
}
However the textarea still get populated with the "\n" characters:
Coping Option: Delete all codes and replace\nSource Proj Num: R21AR058864-02\nSource PI Last Name: SZALAI\nAppl ID: 7924675; File Year: 7924675\n
Thanks.
In java I am trying to replace the \n to
Don't replace the "\n". A JTextArea will parse that as a new line string.
Trying to convert it to a "br" tag won't help either since a JTextArea does not support html.
I always just use code like the following to populate a text area with text:
JTextArea textArea = new JTextArea(5, 20);
textArea.setText("1\n2\n3\n4\n5\n6\n7\n8\n9\n0");
// automatically wrap lines
jTextArea.setLineWrap( true );
// break lines on word, rather than character boundaries.
jTextArea.setWrapStyleWord( true );
From here.
Here is a test that works, try it out:
String str = "This is a test\r\n test.";
if(str.contains("\r\n")) {
System.out.println(str);
}
Assuming Javascript (since you try to replace with a HTML break line):
A HTML textarea newline should be a newline character \n and not the HTML break line <br>. Try to use the code below to remove extra slashes instead of your current if statement and replace. Don't forget to assign the value to the textarea after the replacement.
Try:
str = str.replaceAll("\\n", "\n");
I think your problem is here:
if(str.contains("\\n")){
Instead of "\\n" you just need "\n"
Then instead of "\n" you need "\\n" here:
str = str.replaceAll("(\r\n|\n)", "<br>");
By the way, the if(str.contains() is not really needed because it won't hurt to run replace all if there is no "\n" characters.