Change "," on "." in android - android

I have a string where I have a value: 2,6. How can I change "," on "." I use x.replace(",", "."); but doesn't work. This is any other method to do that?

Try using:
x = x.replace(",",".");
In Java, Strings are immutable, so you will always get a new String from the operations. You have to store this new String, or your changes are lost. replace() returns a new String object, so you need to keep a reference to this new object. Your older String is not modified.

String is immutable it create the new object again after modifying in string. So You need to assign the result.
Do like this.
x= x.replace(",", ".");

Correct way is:
x = x.replace(",", ".");
String is immutable, it can't be changed. x.replace creates a new string

Related

Android : Split string with { character

I have an string
String name = "\"edge_followed_by\":{\"count\":46199005},\"followed_by_viewer\":false,"
I want only this 46199005.
But { shows an error, when try to split the string
String[] separated = name.split("edge_followed_by\":{\"count\":");
Showing a suggestion , number expected and want me to replace with *.
Can anyone help me in this.
Just replace { with \{.
split is trying to use it as a part of regular expression.
Ideally, you should use JSON to parse this if you have proper structure. but if you want to get only the number you can split it using ":" and then split using "}". it should give you the exact number.
Why not to use:
String[] separated = name.split(":");
separated[2].split("}")[0];
Your string is not exact JSON object otherwise you can simply do json parsing and get the count value.
You can get count value using subtring operations like below:
String name = "\"edge_followed_by\":{\"count\":46199005},\"followed_by_viewer\":false,";
String substr = name.substring(name.indexOf("\"count\":") + 10);
String finalstr = substr.substring( 0, substr.indexOf("},"));
Log.d("Extracted_Value", finalstr); // output -> 46199005
There can be multiple ways. This is just one. Hope it will help you!

How can i get few characters from String?

I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);

Why do we need to initialize the key of intent? [duplicate]

This question already has answers here:
Why initialize the key of extra?
(4 answers)
Closed 9 years ago.
Our intents carry data from one activity to another by key value pairs called extras.
We initialize the key (i.e. declare it as a constant and assign it something e.g. public static final String mykey= "something";) before passing it to the intent by using intent.putExtra(mykey, myvalue);
My question is why do we need to assign a value to the key when it is being declared? What is the use of that value? What is the use of ' = "something" ' in public static final String mykey= "something";
I posted a related question, and a respected person (respected because of their valuable answers) said that when a final is declared, a value must be assigned so it is known what the constant is. Sounds like common sense.
But if I simply declare a constant public static final String a; the compiler does not complain at all, which means initializing a final variable with a value is not a must, as long as it is initialized before it is used.
A relevant answer is appreciated. Thank you in advance.
I'm assuming an Intent is backed by a Map.
If you would have an uninitialized variable as the key, this would mean that the value is essentially lost: there's no way of retrieving it since there's no key associated with it (although I believe it might not be possible at all to insert a null key in a map).
You don't have to actually assign this key to a variable: intent.putExtra("somekey", somevalue); works just as fine.
It's just a matter of making sure you don't accidentally use the wrong key.
As an illustration of why it is beneficial to use final variables:
public static void main(String[] args) {
Map<String, Integer> someMap = new HashMap<>();
String theValue = "X";
someMap.put(theValue, 5);
System.out.println("Variable: " + theValue);
System.out.println("Map: " + someMap.get(theValue));
theValue = "Y";
System.out.println("Variable: " + theValue);
System.out.println("Map: " + someMap.get(theValue));
System.out.println("ByValue: " + someMap.get("X"));
}
Output:
Variable: X
Map: 5
Variable: Y
Map: null
ByValue: 5
If theValue would be final, it wouldn't be able to be reassigned and there wouldn't be any problems getting the value from the underlying Map.

create a new local string variable from value of another variable?

Is is possible to create an new local variable from value of another?
e.g. if value of var1 = "button1" can I construct a new local variable like button1type, ie.using the value of var1 to make part of the new variable
Like this?
String foo = "ohai_" + var1; // Would be "ohai_button1"
If you mean name the variable based on the value in var1? No, but you don't need to.
If you need to associate data based on a string (or other) value, consider using a map.

Android String Array Manipulation

I have a lengthy string in my Android program.
What I need is, I need to split each word of that string and copy that each word to a new String Array.
For eg: If the string is "I did android program" and the string array is named my_array then each index should contain values like:
my_array[0] = I
my_array[1] = did
my_array[2] = Android
my_array[3] = Program
A part of program which I did looks like this:
StringTokenizer st = new StringTokenizer(result,"|");
Toast.makeText(appointment.this, st.nextToken(), Toast.LENGTH_SHORT).show();
while(st.hasMoreTokens())
{
String n = (String)st.nextToken();
services1[i] = n;
Toast.makeText(appointment.this, st.nextToken(), Toast.LENGTH_SHORT).show();
}
Can any one please suggest some ideas..
Why not use String.split() ?
You can simply do
String[] my_array = myStr.split("\\s+");
Since '|' is a special character in regular expression, we need to escape it.
for(String token : result.split("\\|"))
{
Toast.makeText(appointment.this, token, Toast.LENGTH_SHORT).show();
}
You can use String.split or Android's TextUtils.split if you need to return [] when the string to split is empty.
From the StringTokenizer API docs:
StringTokenizer is a legacy class that
is retained for compatibility reasons
although its use is discouraged in new
code. It is recommended that anyone
seeking this functionality use the
split method of String or the
java.util.regex package instead.
Since String is a final class, it is by default immutable, which means you cannot make changes to your strings. If you try, a new object will be created, not the same object modified. Therefore if you know in advance that you are going to need to manipulate a String, it is wise to start with a StringBuilder class. There is also StringBuffer for handling threads. Within StringBuilder there are methods like substring():
substring(int start)
Returns a new String that contains a subsequence of characters currently contained in this character sequence.
or getChars():
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Characters are copied from this sequence into the destination character array dst.
or delete():
delete(int start, int end)
Removes the characters in a substring of this sequence.
Then if you really need it to be a String in the end, use the String constructor(s)
String(StringBuilder builder)
Allocates a new string that contains the sequence of characters currently contained in the string builder argument.
or
String(StringBuffer buffer)
Allocates a new string that contains the sequence of characters currently contained in the string buffer argument.
Although to understand when to use String methods and when to use StringBuilder, this link or this might help. (StringBuilder comes in handy with saving on memory).

Categories

Resources