How to add something into string that read by String.xml? - android

I used this:
String message += getResources().getString(R.string.string1) + "some more word...";
and I wanted to send this string via sms, but it is not working. It works fine without the string resource. Am I missing something?

#forpas answer is absolutely correct, but you can also concat string resource this way.
<string name="name">Name %s</string>
String nameText = getString(R.string.name,"khemraj");

When you use += operator with a String the result is a concatenation of the previous value of the String with some new String.
When you define a String variable like this:
String s;
the variable s is not initialized, so this:
s+="something";
is not allowed.
So instead of
String message += getResources().getString(R.string.string1) + "some more word...";
do
String message = getResources().getString(R.string.string1) + "some more word...";

Related

get specific characters of a String

I need to extract specfic information of a String. So I have to create two new Strings with the necessary information isolated.
The structure of the String: {line1=necessary information 1, line2=necessary information 2}
As you can see, I need the String values (necessary information 1: after '=' and before ',' and necessary information 2)
This is the String:
String telefonname_nummer = listview.getItemAtPosition((int) position).toString();
Thanks a lot.
You can try something like this:
String yourInfo = "line1=necessary information 1, line2=necessary information 2";
String[] parts = yourInfo.split(",");
String info1 = parts[0].split("=")[1];
String info2 = parts[1].split("=")[1];
try this way may help you
String urString = "line1=necessary information 1, line2=necessary information 2";
// First split your string with ","
String[] splitedString = urString.split(",");
//as u mention in Qestion u want string after "="
//so,
String firstString = splitedString [0].split("=")[1];
String scndString = splitedString [1].split("=")[1];
Log.e("firstString ",firstString );
Log.e("scndString",scndString);

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);

Subtracting string from a string [Android]

I have following two string:
String one:
"abcabc/xyzxyz/12345/random_num_09/somthing_random.txt"
String Two:
"abcabc/xyzxyz/12345/"
What i want to do is attach path "random_num_09/somthing_random.txt" from string one two string two. So how can i subtract string two from string one and then attach remaining part to string two.
I have tried to do it by searching for the second last "/" in the string one and then doing sub string and attaching it to string two.
But is there any better way of doing it.
Thanks.
I think the best way is to use substrings, as you said:
String string_one = "abcabc/xyzxyz/12345/random_num_09/somthing_random.txt";
String string_two = "abcabc/xyzxyz/12345/";
String result = string_two + string_one.substring(string_one.indexOf(string_two)+1));
The other possibility is to use regex, but you would still be doing concatenation to get the result.
Pattern p = Pattern.compile(string_two+"(.*)");
Matcher m = p.matcher(string_one);
if (m.matches()) {
String result = string_two+m.group(1);
}
rather that a substring, replace is simpler to use:
String string1 = "abcabc/xyzxyz/12345/random_num_09/somthing_random.txt";
String string2 = "abcabc/xyzxyz/12345/";
String res = string2 + string1.replace(string2, "");

read message value from string.xml file in android

i have string like #string/screen_enable_auto_mode. I know that i can use:
String mess = getResources().getString(R.string.screen_enable_auto_mode);
and i will get a message.
But i have whole string as #string/screen_enable_auto_mode. How can i parse it to
String mess = getResources().getString(R.string.screen_enable_auto_mode);
that i will get a message from xml?
You can use Resources.getIdentifier for this.
int resId = getResources().getIdentifier(
"screen_enable_auto_mode", "string", "com.package.app");
String mess = getResources().getString(resId);
Replace "com.package.app" with the actual package of your app, of course.

How to concatenate strings and integer into a variable

It is a real silly questions but I can't get it to work. I've used the search option, but couldn't not find my answer for android.
What I would like to do it the following:
In res/strings.xml i've got several strings
<string name="good0">blablabla</string>
<string name="good1">balablabla2</string>
etc
I want to show those strings randomly in a those when something happens:
Toast.makeText(this,R.string.good+(Math.random()*10), Toast.LENGTH_LONG).show();
But this doesn't work.
Thanks a lot for your help!
Use a String Array.
In strings.xml:
<resources>
<string-array name="messages">
<item>blablabla</item>
<item>blablabla</item>
</string-array>
</resources>
Then, in code you will have something like:
String[] messages = getResources().getStringArray(R.array.messages);
Random r = new Random();
String message = messages[r.nextInt(messages.length)];
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
R.string.good is an int because it refers to a Resource. This int IDENTIFIES a string in an XML file. Android provides a getString() for its resource identifiers.
Android Docs on String Resources
You'll have to get the String out of the resource file this way, then concatenate as normal.
You can't do that.
You will have to use a switch block.
String myString;
switch(Math.random() * 10) {
case 0:
myString = getString(R.string.good1);
break;
}
Toast.makeText(this, myString, Toast.LENGTH_LONG).show();
If you have multiple string values or integer values and you to store it in a single string then String Builder is the best for this
type of operation, For Example you have a string array and you want to
store in a single string and then display this string then use this
method. it will work hundred percent and it is too much suitable for
this type of problems.**
String my_str=null;
StringBuilder bldr=new StringBuilder();
for(int j=0;j<5;j++)
bldr.append(phonearray[j]).append(",");
my_str=bldr.toString();
here in this case i am assigning phone array to a single string and
then i will display it etc...

Categories

Resources