Print Console in Application JavaFX [duplicate] - javafx

How can I make Java print "Hello"?
When I type System.out.print("Hello"); the output will be Hello. What I am looking for is "Hello" with the quotes("").

System.out.print("\"Hello\"");
The double quote character has to be escaped with a backslash in a Java string literal. Other characters that need special treatment include:
Carriage return and newline: "\r" and "\n"
Backslash: "\\"
Single quote: "\'"
Horizontal tab and form feed: "\t" and "\f"
The complete list of Java string and character literal escapes may be found in the section 3.10.6 of the JLS.
It is also worth noting that you can include arbitrary Unicode characters in your source code using Unicode escape sequences of the form \uxxxx where the xs are hexadecimal digits. However, these are different from ordinary string and character escapes in that you can use them anywhere in a Java program ... not just in string and character literals; see JLS sections 3.1, 3.2 and 3.3 for a details on the use of Unicode in Java source code.
See also:
The Oracle Java Tutorial: Numbers and Strings - Characters
In Java, is there a way to write a string literal without having to escape quotes? (Answer: No)

char ch='"';
System.out.println(ch + "String" + ch);
Or
System.out.println('"' + "ASHISH" + '"');

Escape double-quotes in your string: "\"Hello\""
More on the topic (check 'Escape Sequences' part)

You can do it using a unicode character also
System.out.print('\u0022' + "Hello" + '\u0022');

Adding the actual quote characters is only a tiny fraction of the problem; once you have done that, you are likely to face the real problem: what happens if the string already contains quotes, or line feeds, or other unprintable characters?
The following method will take care of everything:
public static String escapeForJava( String value, boolean quote )
{
StringBuilder builder = new StringBuilder();
if( quote )
builder.append( "\"" );
for( char c : value.toCharArray() )
{
if( c == '\'' )
builder.append( "\\'" );
else if ( c == '\"' )
builder.append( "\\\"" );
else if( c == '\r' )
builder.append( "\\r" );
else if( c == '\n' )
builder.append( "\\n" );
else if( c == '\t' )
builder.append( "\\t" );
else if( c < 32 || c >= 127 )
builder.append( String.format( "\\u%04x", (int)c ) );
else
builder.append( c );
}
if( quote )
builder.append( "\"" );
return builder.toString();
}

System.out.println("\"Hello\"");

System.out.println("\"Hello\"")

There are two easy methods:
Use backslash \ before double quotes.
Use two single quotes instead of double quotes like '' instead of "
For example:
System.out.println("\"Hello\"");
System.out.println("''Hello''");

Take note, there are a few certain things to take note when running backslashes with specific characters.
System.out.println("Hello\\\");
The output above will be:
Hello\
System.out.println(" Hello\" ");
The output above will be:
Hello"

Use Escape sequence.
\"Hello\"
This will print "Hello".

you can use json serialization utils to quote a java String.
like this:
public class Test{
public static String quote(String a){
return JSON.toJsonString(a)
}
}
if input is:hello output will be: "hello"
if you want to implement the function by self:
it maybe like this:
public static String quotes(String origin) {
// 所有的 \ -> \\ 用正则表达为: \\ => \\\\" 再用双引号quote起来: \\\\ ==> \\\\\\\\"
origin = origin.replaceAll("\\\\", "\\\\\\\\");
// " -> \" regExt: \" => \\\" quote to param: \\\" ==> \\\\\\\"
origin = origin.replaceAll("\"", "\\\\\\\"");
// carriage return: -> \n \\\n
origin = origin.replaceAll("\\n", "\\\\\\n");
// tab -> \t
origin = origin.replaceAll("\\t", "\\\\\\t");
return origin;
}
the above implementation will quote escape character in string but exclude
the " at the start and end.
the above implementation is incomplete. if other escape character you need , you can add to it.

Related

How to remove the new line when reading from UNIX process groovy? [duplicate]

I have a string that contains some text followed by a blank line. What's the best way to keep the part with text, but remove the whitespace newline from the end?
Use String.trim() method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.
String trimmedString = myString.trim();
String.replaceAll("[\n\r]", "");
This Java code does exactly what is asked in the title of the question, that is "remove newlines from beginning and end of a string-java":
String.replaceAll("^[\n\r]", "").replaceAll("[\n\r]$", "")
Remove newlines only from the end of the line:
String.replaceAll("[\n\r]$", "")
Remove newlines only from the beginning of the line:
String.replaceAll("^[\n\r]", "")
tl;dr
String cleanString = dirtyString.strip() ; // Call new `String::string` method.
String::strip…
The old String::trim method has a strange definition of whitespace.
As discussed here, Java 11 adds new strip… methods to the String class. These use a more Unicode-savvy definition of whitespace. See the rules of this definition in the class JavaDoc for Character::isWhitespace.
Example code.
String input = " some Thing ";
System.out.println("before->>"+input+"<<-");
input = input.strip();
System.out.println("after->>"+input+"<<-");
Or you can strip just the leading or just the trailing whitespace.
You do not mention exactly what code point(s) make up your newlines. I imagine your newline is likely included in this list of code points targeted by strip:
It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
It is '\t', U+0009 HORIZONTAL TABULATION.
It is '\n', U+000A LINE FEED.
It is '\u000B', U+000B VERTICAL TABULATION.
It is '\f', U+000C FORM FEED.
It is '\r', U+000D CARRIAGE RETURN.
It is '\u001C', U+001C FILE SEPARATOR.
It is '\u001D', U+001D GROUP SEPARATOR.
It is '\u001E', U+001E RECORD SEPARATOR.
It is '\u001F', U+0
If your string is potentially null, consider using StringUtils.trim() - the null-safe version of String.trim().
If you only want to remove line breaks (not spaces, tabs) at the beginning and end of a String (not inbetween), then you can use this approach:
Use a regular expressions to remove carriage returns (\\r) and line feeds (\\n) from the beginning (^) and ending ($) of a string:
s = s.replaceAll("(^[\\r\\n]+|[\\r\\n]+$)", "")
Complete Example:
public class RemoveLineBreaks {
public static void main(String[] args) {
var s = "\nHello world\nHello everyone\n";
System.out.println("before: >"+s+"<");
s = s.replaceAll("(^[\\r\\n]+|[\\r\\n]+$)", "");
System.out.println("after: >"+s+"<");
}
}
It outputs:
before: >
Hello world
Hello everyone
<
after: >Hello world
Hello everyone<
I'm going to add an answer to this as well because, while I had the same question, the provided answer did not suffice. Given some thought, I realized that this can be done very easily with a regular expression.
To remove newlines from the beginning:
// Trim left
String[] a = "\n\nfrom the beginning\n\n".split("^\\n+", 2);
System.out.println("-" + (a.length > 1 ? a[1] : a[0]) + "-");
and end of a string:
// Trim right
String z = "\n\nfrom the end\n\n";
System.out.println("-" + z.split("\\n+$", 2)[0] + "-");
I'm certain that this is not the most performance efficient way of trimming a string. But it does appear to be the cleanest and simplest way to inline such an operation.
Note that the same method can be done to trim any variation and combination of characters from either end as it's a simple regex.
Try this
function replaceNewLine(str) {
return str.replace(/[\n\r]/g, "");
}
String trimStartEnd = "\n TestString1 linebreak1\nlinebreak2\nlinebreak3\n TestString2 \n";
System.out.println("Original String : [" + trimStartEnd + "]");
System.out.println("-----------------------------");
System.out.println("Result String : [" + trimStartEnd.replaceAll("^(\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029])|(\\r\\n|[\\n\\x0B\\x0C\\r\\u0085\\u2028\\u2029])$", "") + "]");
Start of a string = ^ ,
End of a string = $ ,
regex combination = | ,
Linebreak = \r\n|[\n\x0B\x0C\r\u0085\u2028\u2029]
Another elegant solution.
String myString = "\nLogbasex\n";
myString = org.apache.commons.lang3.StringUtils.strip(myString, "\n");
For anyone else looking for answer to the question when dealing with different linebreaks:
string.replaceAll("(\n|\r|\r\n)$", ""); // Java 7
string.replaceAll("\\R$", ""); // Java 8
This should remove exactly the last line break and preserve all other whitespace from string and work with Unix (\n), Windows (\r\n) and old Mac (\r) line breaks: https://stackoverflow.com/a/20056634, https://stackoverflow.com/a/49791415. "\\R" is matcher introduced in Java 8 in Pattern class: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
This passes these tests:
// Windows:
value = "\r\n test \r\n value \r\n";
assertEquals("\r\n test \r\n value ", value.replaceAll("\\R$", ""));
// Unix:
value = "\n test \n value \n";
assertEquals("\n test \n value ", value.replaceAll("\\R$", ""));
// Old Mac:
value = "\r test \r value \r";
assertEquals("\r test \r value ", value.replaceAll("\\R$", ""));
String text = readFileAsString("textfile.txt");
text = text.replace("\n", "").replace("\r", "");

PHP's explode() is not working on hyphen-minus -

I am try to use explode() on a string fetched from a database but it didn't work. I have tried explode('-',$string) but it's still not working.
Here is my string which I want to explode:
Expression of Interest – Join our Paint Team – North
If you look closely the hyphen from the string is not the same as the hyphen you use as your argument for the explode.
The hyphen in the string is the following – while the hyphen you pass as the argument for explode() is -. As you can see they don't match (the one in the string is longer than the one you try to compare it with). Because the characters don't match, the explode function is returning the whole string.
<?php
$string = "Expression of Interest – Join our Paint Team – North";
$strings = explode('–', $string);
var_dump($strings);
I have copied the hyphen from the text and used that as an argument for explode() and it works fine.
Might be $string is not string, you can use strval( $string ) to convert it to string i.e. explode('–', strval ( $string ) );
$eString = explode('–', strval ( $string ) );
// vardump($eString) - Now it is an array.
// echo $eString[0];
I have fix the issue by trying this
$post_job_title = htmlentities(get_the_title($posts));
$post_job_title = explode (" – ", $post_job_title);

ASP.Net Regular Expression Validation of String Length

I want to validate if the string entered by user into a text box is at least 3 characters long. I was using following regex:
\w{3,40}
But it fails if user enters say:
my valid string
I want something that will validate the total string length including spaces between the words but not at end or beginning of the string.
So following are valid:
"a bc"
"a b c"
"abc"
"a b c"
but following are not:
"a b "
" a "
" bc"
NOTE:- It is to be done on client side as any changes to server side code will require restarting site which I don't want to do.
Call Trim method in the input and try to use this as your regex pattern:
[\w\s]{3,40}
\s will matches all whitespaces characters.
I test it in here
More: Regular Expression Basic Syntax Reference
I'm guessing you are just having fun with regular expressions but the following check should work:
if ( s.Trim().Length >= 3 )
{
// do awesome stuff here that you would never do with some other crazy string
}
I am still waiting for the working regex. I have tried following, but it fails if there is a space at end of the string:
^[\S|\S\s]{2,40}\S$
Meanwhile I am using following approach to passon custom validation function:
function pageLoad() {
var val = document.all ? document.all["<%=control.ClientID%>"] :
document.getElementById("<%=control.ClientID%>");
if (val) {
val.evaluationfunction = function () {
try {
var v = ValidatorGetValue(val.controltovalidate);
v = ValidatorTrim(v);
return v.length >= 3;
} catch (err) {
return true;
}
};
}
}
Note that most of it is picked from Microsoft ASP.Net Javascript Validation library itself.
Try this and let me know if it works...
Note: Not tested
[A-Za-z]{3}([A-Za-z]+ ?)*

how to write the regular expression for the following details?

How do I write a regular expression which should allow only [], ', /,\, space, +,-,*,(),{},&,^ and #?
i want regular expression which work in dotnet.
Please help me?
This should do it
/[[\]'/\\# ]+/
Explanation
NODE EXPLANATION
--------------------------------------------------------------------------------
[[\]'/\\# ]+ any character of: '[', '\]', ''', '/',
'\\', '#', ' ' (1 or more times (matching
the most amount possible))
Notes:
\] is escaped because it appears inside a bracket ([]) pair
\\ is escaped because \ is the escape character
Update per your comment
/[[\]'/\\# &(){}+$%#=~"-]+/
To match 1 or more of the characters:
[[\]'/\\# ]+
To also match the empty string, change the + to a *, i.e.
[[\]'/\\# ]*
Try this out for C#.NET:
using System;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string txt=",'/\\ #";
string re1=".*?"; // Non-greedy match on filler
string re2="(#)"; // Any Single Character 1
Regex r = new Regex(re1+re2,RegexOptions.IgnoreCase|RegexOptions.Singleline);
Match m = r.Match(txt);
if (m.Success)
{
String c1=m.Groups[1].ToString();
Console.Write("("+c1.ToString()+")"+"\n");
}
Console.ReadLine();
}
}
}
Hope it helps :)

ASP.net validator regular expression and accented names / characters

I have a asp.net control that is using a regular expression to validate the users input for first name and last name. It works for up to 40 characters...and I think by the looks of the expression it also allows ' for names like O'Donald and maybe hypenated names too.
ValidationExpression="^[a-zA-Z''-'\s]{1,40}$"
My problem is with accented names/characters e.g. Spanish and French names that may contain for example ñ are not allowed. Does anyone know how to modify my expression to take this into account?
You want
\p{L}: any kind of letter from any language.
From regular-expressions.info
\p{L} or \pL is every character in the unicode table that has the property "letter". So it will match every letter from the unicode table.
You can use this within your character class like this
ValidationExpression="^[\p{L}''-'\s]{1,40}$"
Working C# test:
String[] words = { "O'Conner", "Smith", "Müller", "fooñ", "Fooobar12" };
foreach (String s in words) {
Match word = Regex.Match(s, #"
^ # Match the start of the string
[\p{L}''-'\s]{1,40}
$ # Match the end of the string
", RegexOptions.IgnorePatternWhitespace);
if (word.Success) {
Console.WriteLine(s + ": valid");
}
else {
Console.WriteLine(s + ": invalid");
}
}
Console.ReadLine();

Resources