I'm starting with Android Studio and making a simple App that take an input from am EditText and check if have four characters long(the easy part) and if the whole string is an hexadecimal value like "FFA1".
I think in to add the "0x" to the String but that use more resources a far a read in another post/questions.
I also read that exist a library called TextUtils with a function isDigitsOnly() but I'm not sure if they call digits to 0-9 values or 0-9 and A-F too.
In VB.NET I use to add &H to the String and use isNumeric() function to detect Hexa values, but in android I'm lost.
Can somebody enlighten me?
Thanks.
You should to test with this void:
private void test(){
try{
String hex = "Your String"
int hexadecimalValue = Integer.parseInt(hex, 16);
System.out.println("valid hexadecimal");
} catch(NumberFormatException e){
// not a valid hex
System.out.println("not a valid hexadecimal");
}}
Related
I want to put extra value from intent to other intent. But in other intent, app get all value. Example:
mAddress.setText(" from " + address);
String put_address = mAddress.getText().toString();
editIntent.putExtra("put_address", put_address);
is it possible to cut text "from" and get only address variable ???
you can split a string like
str = "From address#dd.com";
String modified = str.replace;
now splitstr contain your split strings
splitStr[1] contains "address#dd.com"
Can also use
str.substring(str.indexOf(" ")+1);
By the way, you can use jagapathi's answer. In his example he uses regular expression.
Regular expressions can help to parse, find, cut substrings using a particular pattern. In his code he splits string by any space character.
But, imho, the simplest solution is to create a substring using this code:
'put_address.substring(7);'
use one of these solutions:
String input = put_address.trim().substring(5);
*** note: 5 is index of real address first character;
String input = put_address..split(" ")[1];
I use this code for replace this • character with \n in textview in android
TextView tvcontent=(TextView) row.findViewById(R.id.row_comment_content);
tvcontent.setText(content[position].replace("•", "\n"));
Now I want to replace this char in the image http://i.stack.imgur.com/cnJeI.jpg
but I don't know what is the ASCII code of that char in the image to replace in android.
If you know what is ASCII code of the char please help.
If you mean a middot char, its code is 183.
Also, maybe this link can help.
Ok, now I got what you want.
This page would answer your question. Brief extract from this page:
this is OBJECT REPLACEMENT CHARACTER and it's code in Java is \uFFFC.
Also I typed it in Android Studio - it works (screen below):
If you possibly need it in HTML - it's code is  ().
And don't forget to use single quote when you will replace this char!
Use this code:
String a = "your content with dots";
String b = a.replace("dots", "\n");
textview.settext(b);
Try running this code to find the ascii value of any character. But for this bullet I don't think there is any ascii code.
public class HelloWorld{
public static void main(String []args){
String s= "•";
int a = s.charAt(0);
System.out.println("Ascii code is "+ a);
}
}
Here when we run this code then compiler shows an error:
unmapped character for encoding ascii.
But in future if you want to know ascii code of any character that is actually ascii, just use this function.
Users can choose a own downloadpath for their data and i want the 'path' or 'directory' string to be checked on illegal characters (!##$%^&*, etc) and if possible replace them.
Can somebody help pls?
Thank you in advance.
public void onDownloadLocationChanged(final String newLocation) {
//
final String original = settings.getDownloadsLocation();
//
if (!newLocation.equals(original)) {
//
if (new File (newLocation).isDirectory() && (new File(newLocation).exists())) {
// DoSomething
}}
You could achieve this by using the replaceAll String method. For example, if you wanted to remove the "#" character from your string, you could do the following:
original.replaceAll("[\\#]", "");
The call above will replace all of the occurences of "#" within the string with a "", which essentially means that the character was deleted. Furthermore, you can add any character you wish to remove to the call above. If you wish to use a different replacement for each bad character, then you would need to consider multiple replaceAll statements, but if you simply want to remove illegal characters from your string then this is the way to go.
I want to have a parameter in one string in strings.xml and this parameter should be a double value. So I use %1$f. Here - http://developer.android.com/reference/java/util/Formatter.html there are many examples, but what if I want to have have a few double/float parameters and I want only the second one to have 2 digits after .? I tried to use combinations like %2$.2f or %2.2$f. Nor of them worked. %.1f does not work as well.
So, does anybody know how can I "customize" a float/double value inside a strings.xml? Thanks.
Just adding to #David Airam's answer here; the "incorrect" solution he gives is actually correct, but with a bit of tweaking. The XML file should contain:
<string name="resource1">Hello string: %1$s, and hello float: %2$.2f.</string>
Now in the Java code:
String svalue = "test";
float sfloat= 3.1415926;
String sresult = getString(R.string.resource1, svalue, sfloat);
The exception that #David Airam reported is from trying to jam a String into a format specifier with %f, which requires a floating point type. Use float and there is no such exception.
Also, you can use Float.valueOf() to convert a String to a float in case your input data was originally a string (say, from a EditText or something). However, you should always try/catch valueOf() operations and handle the NumberFormatException case, since this exception is unchecked.
%.1f work for me if you like to show only 1 digit after ','
Define is strings.xml file
<string name="price_format">$%,.2f</string>
//For using in databinding where amount is double type
android:text="#{#string/price_format(model.amount)}"
//For using in java runtime where priceOfModifier is double type
amountEt.setText(context.getResources().getString(R.string.price_format, priceOfModifier));
This worked for me.
<string name="market_price">Range ₹%1$.0f - ₹%2$.0f</string>
android:text="#{#string/market_price(viewModel.suggestedPriceRange.max, viewModel.suggestedPriceRange.min)}"
Outputs: Range ₹500 - ₹1000
In ₹%1$.0f, .0f defines how many digits you want after the decimal.
A simpler approach:
<string name="decimalunit">%.2f%n</string>
float sfloat= 3.1475926;
String sresult = getString(R.string.decimalunit, sfloat);
Output: 3.15
If it were me I'd store the values in the resources as simple values, and then use formatter methods to control how they're displayed, roughly like this
public String formatFigureTwoPlaces(float value) {
DecimalFormat myFormatter = new DecimalFormat("##0.00");
return myFormatter.format(value);
}
public String formatFigureOnePlace(float value) {
DecimalFormat myFormatter = new DecimalFormat("##0.0");
return myFormatter.format(value);
}
I now that this reply is arriving too late... but I hope to be able to help other people:
Android sucks with multiple parameters substitutions when you want decimal numbers and format this in common style %a.bf
The best solution I have found (and only for these kind of resources) is put the decimal parameters as strings %n$s and in the code apply my conversion with String.format(...)
Example:
INCORRECT WAY:
// In xml file:
<string name="resource1">You has a desviation of %1$s and that is a %2$.2f%% percentage.</string>
// And in java file
String sresult = getString(R.string.resource1, svalue, spercentage); // <-- exception!
This solution is technically correct but incorrect due to android substitution resources system so the last line will generate an exception.
CORRECT WAY / SOLUTION:
Simply convert the second parameter into a String.
<string name="resource1">You has a desviation of %1$s and that is a %2$s percentage.</string>
And now in the code:
...
// This is the auxiliar line added to solve the problem
String spercentage = String.format("%.2f%%",percentage);
// This is the common code where we use the last variable.
String sresult = getString(R.string.resource1, svalue, spercentage);
i have a String displayed on a WebView as "Siwy & Para Wino"
i fetch it from url , i got a string "Siwy%2B%2526%2BPara%2BWino". // be corrected
now i'm trying to use URLDecoder to solve this problem :
String decoded_result = URLDecoder.decode(url); // the url is "Siwy+%26+Para+Wino"
then i print it out , i still saw "Siwy+%26+Para+Wino"
Could anyone tell me why?
From the documentation (of URLDecoder):
This class is used to decode a string which is encoded in the application/x-www-form-urlencoded MIME content type.
We can look at the specification to see what a form-urlencoded MIME type is:
The form field names and values are escaped: space characters are replaced by '+', and then reserved characters are escaped as per [URL]; that is, non-alphanumeric characters are replaced by '%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks, as in multi-line text field values, are represented as CR LF pairs, i.e. '%0D%0A'.
Since the specification calls for a percent sign followed by two hexadecimal digits for the ASCII code, the first time you call the decode(String s) method, it converts those into single characters, leaving the two additional characters 26 intact. The value %25 translates to % so the result after the first decoding is %26. Running decode one more time simply translates %26 back into &.
String decoded_result = URLDecoder.decode(URLDecoder.decode(url));
You can also use the Uri class if you have UTF-8-encoded strings:
Decodes '%'-escaped octets in the given string using the UTF-8 scheme.
Then use:
String decoded_result = Uri.decode(Uri.decode(url));
thanks for all answers , i solved it finally......
solution:
after i used URLDecoder.decode twice (oh my god) , i got what i want.
String temp = URLDecoder.decode( url); // url = "Siwy%2B%2526%2BPara%2BWino"
String result = URLDecoder.decode( temp ); // temp = "Siwy+%26+Para+Wino"
// result = "Swy & Para Wino". !!! oh good job.
but i still don't know why.. could someone tell me?