sachit
sachit

Reputation: 1138

Get Array from string.xml

I have a string in my string.xml whose structure is like this.

string.xml

<string name="my" formatted="false">As*#*Bs*#*Cs</string>

and I am fetching this string in my main file like this.

main.java

String h1 = getResource().getString(R.string.my);

and my output is like this.

h1=As*#*Bs*#*Cs

but I want output in an array without regex like this.

h1[0]="As",h1[1]="Bs",h1[2]="Cs";

What should I change to get the above output? Any help would be appreciated

Upvotes: 1

Views: 263

Answers (3)

user370305
user370305

Reputation: 109257

Have you tried?

String[] stringArray = h1.split("\\*#\\*");

Upvotes: 1

Leeeeeeelo
Leeeeeeelo

Reputation: 4383

I advise you to store your string as a string array in the following manner :

<resources>
    <string-array name="my">
        <item>As</item>
        <item>Bs</item>
        <item>Cs</item>
    </string-array>
</resources>

You will be able to retrieve you array in the following manner :

String [] h1 = getResources().getStringArray ( R.array.my );

Upvotes: 1

Chirag
Chirag

Reputation: 56935

After stroring your xml string in h1 String . You need to apply split on h1 string.

Try this.

String h1= "As*#*Bs*#*Cs";
String[] test = h1.split("\\*#\\*"); 

for(String str : test)
{
      Log.i("==============", str);
}

Output :

I/==============( 1321): As
I/==============( 1321): Bs
I/==============( 1321): Cs

You can use str[0] , str[1] and str[2] to get respective string.

Upvotes: 0

Related Questions