I want to print an attribute of a class I am making , but I need it to be printed inside quotes " ".
I know it has something to with Escape Sequences but a similar post I found suggested using "\"Hello\"" for example to print "Hello"... My case is a bit more complicated cause I don't know beforehand the value of the attribute I want to print. So how can I do this?
Why don't make a function that make any String into a Quote. Exanple
public static String quotePrinter(String myQuote)
{
return "\"" +myQuote+ "\"";
}
String myQuote = "Hello World";
System.out.println(quotePrinter(myQuote));
And the output
"Hello World"
Not sure if i understand corretly want you want but take a look at the answers from #ataylor and #Martin Törnwall How to format strings in Java for String interpolation
Related
I tried to make security to display email data by replacing some words with symbol (*) but not as expected there might be an error in making the example script as below.
String email = "thismyemail#myhost.com";
String get_text = email.get_text(3, 6);
String hasil = email.replace(get_text,"*");
email_string = (EditText) findViewById(R.id.emailT);
email_string.setText(hasil);
But the result is like this
thi*email#myhost.com
Which I expect
thi***email#myhost.com
String hasil = email.replace(get_text,"***");
But please note that if that text appears anywhere else in the string it will be replaced as well.
Also, if the email is like jf#mymailserver.com you won't be replacing a part of their user id with *.
So you can probably find a better way to select the characters, taking into account email length and also not "replacing" text but rather putting those chars at the specific position you want to.
See this related question for some ideas on how to improve this:
masking of email address in java
Your code seems right. If ur expected output is like the one mentioned above, you can just add 2 more "*" to the code.
String hasil = email.replace(get_text,"***");
I hope it helps
I am using a string as my source for an equation, and whenever I try to add something like an overbar tag which is:
\ov5\ - creates a bar over the 5
However, when I add this into a Java string, for it to compile I am required to write it like this:
string x= "\\ov5\\";
It would appear that this way breaks JQMath and doesn't work, resulting in a broken equation. Here is the code in case I did something terribly wrong:
WebView webView;
String functext = "$$\\ov55\\$$";
js = "<html><head>"
+ "<link rel='stylesheet' href='file:///android_asset/mathscribe/jqmath-0.4.3.css'>"
+ "<script src='file:///android_asset/mathscribe/jquery-1.4.3.min.js'></script>"
+ "<script src='file:///android_asset/mathscribe/jqmath-etc-0.4.3.min.js'></script>"
+ "</head><body>"
+ functext + "</body></html>";
webView.loadDataWithBaseURL("", js, "text/html", "UTF-8", "");
EDIT: For clarification, the end result oddly reads "$$\ov55$$".
Please note that when I try the same string on JQMath's website page here, it works as intended.
EDIT2: Here are some debug values for a breakpoint placed at webView.loadDataWithBaseURL:
actual string: String functext = "$$\\\\ov55\\\\$$";
actual displayed result: $$\ov55\$$
debug results:
functext = $$\\ov55\\$$
js = <html><head><link rel='stylesheet' href='file:///android_asset/mathscribe/jqmath-0.4.3.css'><script src='file:///android_asset/mathscribe/jquery-1.4.3.min.js'></script><script src='file:///android_asset/mathscribe/jqmath-etc-0.4.3.min.js'></script></head><body>$$\\ov55\\$$</body></html>
Any help with loading it in another way other than a string would help greatly.
I think you want this:
String functext = "$$\\ov55\\$$";
(The first \ needs to be before the ov operator.)
EDIT: Another possibility (since the above was evidently just a typo in your post, not in your code) is that somewhere in the pipeline the string is being interpolated a second time. In that case, you would need to double-escape the backslashes:
String functext = "$$\\\\ov55\\\\$$";
P.S. If the end result reads "$$\ov55$$" then the problem seems to be before jqmath sees anything. The code you posted definitely does not produce that result for me.
Also jqMath accepts ` (backquote) in place of \ if that makes things easier. Finally, I'd put a space between the ov and the 5 to clarify that it's not a macro named ov5. (Plus see my comment above to remove the final \.)
I have a string formatted as below:
source1.type1.8371-(12345)->source2.type3.3281-(38270)->source4.type2.903..
It's a path, the number in () is the weight for the edge, I tried to split it using java Pattern as following:
[a-zA-Z.0-9]+-{1}({1}\\d+){1}
[a-zA-Z_]+.[a-zA-Z_]+.(\\d)+-(\\d+)
[a-zA-Z.0-9]+-{1}({1}\\d+){1}-{1}>{1}
hopefully it split the string into fields like
source1.type1.8371-(12345)
source2.type3.3281-(38270)
..
but none of them work, it always return the whole string as the field.
It looks like you just want String.split("->") (javadoc). This splits on the symbol -> and returns an array containing the parts between ->.
String str = "source1.type1.8371-(12345)->source2.type3.3281-(38270)->source4.type2.903..";
for(String s : str.split("->")){
System.out.println(s);
}
Output
source1.type1.8371-(12345)
source2.type3.3281-(38270)
source4.type2.903..
It seems to me like you want to split at the ->'s. So you could use something like str.split("->") If you were more specific about why you need this maybe we could understand why you were trying to use those complicated regexes
I am not posting any code I am struck with. I am trying this in Java:
Issue:
I have words like:
,xxxx-1223
yyyyy,xxdd-345
$,xxxxr-7
sdsdsdd-18
so what ever format I have I should be able to read the last one:
xxxx-1223
xxdd-345
xxxxr-7
sdsdsdd-18
what so may be the words, all I need to to get the words as shown.
Use String#lastIndexOf(int) to find where the last comma occurs, and use String#substring(int) to get the rest of the string that follows.
String input = /* whatever */;
int lastComma = input.lastIndexOf(',');
String output = input.substring(lastComma + 1);
String[] str=yourWord.split(",");
String output=str[str.length-1];
You can use this Regex: -
(\\w+-\\d+)$
Or this specific problem can simply be solved using String.split() or String.substring(int) methods
I am using Formatter to output Java code to a file. I want to add a specific number of spaces to the start of each line. My problem is I cannot find a way to do this "neatly". The standard options seem to only allow adding a minimum number of spaces but not a specific number of spaces.
As a work around, I am currently doing the following:
out.format("%7s%s", "", "My text"); but I'd like to do it with only two arguments like this out.format("%7s", "My text");.
Does anyone know if there is a way to do this using the standard Formatter options?
I'm not exactly sure what you want here:
out.format("xxx%10sxxx", "My text");
// prints: xxx My textxxx
While:
out.format("xxx%-10sxxx", "My text");
// prints: xxxMy text xxx
As far as I know, there is no way to do the old C-style formatting to specify the size in an argument like "%*s" because then you could pass in (str.length() + 7).
I'm afraid that your way seems to the the most "neat". If you can explain why you don't like it maybe we can find a better workaround.
You can prepend text into your string.
Another way to reapet any string which you can use this code:-
String str = "abc";
String repeated = StringUtils.repeat(str, 3);
here StringUtils is org.apache.commons.lang3.StringUtils class.
Use Commons Lang
String line = "Hello World!";
int numberOfSpaces = 2;
String lineWithSpacePadding = StringUtils.leftPad(line, line.length() + numberOfSpaces);