How can is use string formatting on the
<string-array><item> - resource in Android?
Should i do it like in the following example(and how should i do it if yes)...
<string-array name="notification-msgs">
<item>%s sent you a message.</item>
<item>%s answered to your Story</item>
</string-array>
or what kind of technique is common to use in this case?
I only know how to Format string resources.(with getString(R.id.mystring,replacevalue))
Thanks for any help!
EDIT:
I tried to use getStringArray(), but this method does not accept more then one argument, which just is an Array-Ressource.
Unfortunately there is no direct way provided by Android to achieve this. Best you can do is:
String.format(getResources().getStringArray(R.array.notification-msgs)[0], "Someone")
Android supports formatting with getString(String str,Object... args). Link here
Or you can wrap formatting with a method.
public static String formatString(String pure,Object... args){
return String.format(pure,args);
}
//Or
public static String formatString(int id,Context context,Object... args){
String pure = context.getString(id);
return String.format(pure,args);
}
I personally prefer second approach, it is easy to mutable in the future.
Related
I want to replace Encode::forUri with something that is not depricated. Does anyone know what method should I use to cover that up? I can't use forUriComponent() because I have to convert an link, not a query, and also forUriComponent() doesn't convert all the characters as forUri() did
Code example:
private static final ImmutableList<UnaryOperator> ENCODING_CHAIN_URL_IN_HTML_ATTRIBUTE = ImmutableList.of(Encode::forUri, EsEncode::uriAsSafeSchemeUri, Encode::forHtml);
Thanks!
I think you should use
URLEncoder.encode("NAME", "UTF-8");
https://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html#encode-java.lang.String-java.lang.String-
Example
URLEncoder.encode(
"urlStringParams",
java.nio.charset.StandardCharsets.UTF_8.toString()
)
I am working with some legacy code that has a static method call which we need to remove from our source tree.
The existing code is as follows:
Logger.getInstance(JdkUtil.forceInit(SomeBusiness.class));
What we need to end up with is:
Logger.getInstance(SomeBusiness.class);
I've spent all day today trying to figure out how to do that replacement. Since I have very little experience with regular expressions, I have only been able to come up with a pattern that matches the source string.
The pattern JdkUtil.forceInit([a-zA-Z_0-9]*.class) finds matches on the input string I am providing. I've tested this at https://www.freeformatter.com/java-regex-tester.html
So if anyone can post a Java solution to this, I would really appreciate it.
Below is some Groovy code that I have so far. What I am missing is to how correctly replacement explained above.
String source = 'Logger.getInstance(JdkUtil.forceInit(RtpRuleEngineCompiledImpl.class))'
String regexpPattern = 'JdkUtil.forceInit\\([a-zA-Z_0-9\\)]*.class\\)'
String replaced = source.replaceFirst(regexpPattern, 'hello')
println replaced
When I run the above code I get the following output:
Logger.getInstance(hello)
Obviously 'hello' is just for testing.
Thanks in advance to anyone who can give me some suggestions.
You'll likely want to do something such as:
class StackOverflow {
public static void main(String[] args) {
String source = "Logger.getInstance(JdkUtil.forceInit(RtpRuleEngineCompiledImpl.class))";
String regexpPattern = "JdkUtil.forceInit\\(([a-zA-Z_0-9]*.class)\\)";
String replaced = source.replaceFirst(regexpPattern, "$1");
System.out.println(replaced);
}
}
Result:
Logger.getInstance(RtpRuleEngineCompiledImpl.class)
The capture group ($1) replaces the entire string which was within the parentheses.
๐ how can I convert emojis like this to text? I mean to convert a happy face to the words "happy" and so on. Using Java, how can I achieve this?
You may use emoji4j library.
String text = "A ๐ฑ, ๐ถ and a ๐ญ became friendsโค๏ธ. For ๐ถ's birthday party, they all had ๐s, ๐s, ๐ชs and ๐ฐ.";
EmojiUtils.shortCodify(text); //returns A :cat:, :dog: and a :mouse: became friends:heart:. For :dog:'s birthday party, they all had :hamburger:s, :fries:s, :cookie:s and :cake:.
Since that emoji is simply a standard Unicode code point (U+1F601, see here), probably the best way is to set up a map which translates them into strings.
By way of example, here's a piece of code that creates a string-to-string map to allow you to look up and translate that exact code point:
import java.util.HashMap;
import java.util.Map;
class Xyzzy {
public static Map<String,String> xlat = new HashMap<String, String>();
public static void main (String[] args) {
xlat.put("\uD83D\uDE01", "happy");
System.out.println(xlat.get("\uD83D\uDE01"));
}
}
You can add as many code points to the map as you wish, and use Map.get() to extract the equivalent text for any of them.
I want to ask, is it possible to get full line method using AST Parser in java file?
example:
public double getAverage(int[] data) {
}
i only get method name (getAverage) using MethodDeclaration, while i hope full line (public double getAverage(int[] data) {
The second question, how to read closing of the method ( } ) ?
Thanks :)
There is no direct way to do that but you can get all the required information and build the string yourself.
You can use MethodDeclaration.getModifiers() to get the modifier information which will tell you whether it is public or private.
You can use MethodDeclaration.getReturnType2().resolveBinding().getName() to get the name of the return type
and MethodDeclaration.parameters() will give you information about parameters.
one more trick to do is :
String signature= MethodDeclaration.toString().split("{")[0];
but this may not be an efficient way to do.
Thank you
I when i use reader.readLine(), the string length is always 80 chars and after the main string unicode spaces are padded up.
Is there a way to remove those unwanted characters.
(java.io.RandomAccessFile reader)
String.trim is not working on this
You can use StringUtils.strip from Commons Lang. It is Unicode-aware.
You can write a custom method in Java to remove the Unicode space characters , using Character.isWhitespace(char) and Character.isSpaceChar(char) methods, for your specific purpose.
The Spring framework has a StringUtils class with a trimWhitespace(String) method which appears to be based on Character.isWhitespace(char) from the source code here.
use Google Guava
CharMatcher.WHITESPACE.trimFrom(source);
or try this https://gist.github.com/michalbcz/4861a2b8ed73bb73764e909b87664cb2
If you do not want a big libs. Just use:
str.replaceAll("^[\\s\\p{Z}]+|[\\s\\p{Z}]+$", "");
Testing
public static String trim(String str) {
return str.replaceAll("^[\\s\\p{Z}]+|[\\s\\p{Z}]+$", "");
}
public static void main(String[] args) {
System.out.println(trim("\t tes ting \u00a0").length());
System.out.println(trim("\t testing \u00a0").length());
System.out.println(trim("tes ting \u00a0").length());
System.out.println(trim("\t tes ting").length());
}
would have been faster to just search stackoverflow for this question becoz there are multiple questions on that topic there. well, try this:
st.replaceAll("\\s","")
check this one here: link