hi to all
i have a couple of text lines where they look like this
You are a friend:
name= [1]
From= [ 2011-02-28 07:00:52]
To= [2011-03-17 07:01:02]
Link= [http://www.example.com]
type= [good] for some
and i want to read only the text that is between the brackets "[]" i would like to know the best way to read it is it by using split(string) or using indexof(string) is better and if there is an example that would be great and also can someone please explane to me how does this code read the text that is between the brackets
thank you
String s = addressString;
int be = s.indexOf('[');
int e = s.indexOf(String[] fields = s.substring(s.indexOf('['), s.indexOf(']'));
i figured out the code i posted it should be like this
String s = addressString;
int be = s.indexOf('[');
int e = s.indexOf(']');
String fields = s.substring(be+1, e);
but now i have another problem which i have more than one line so when i change it to String[] fields it gives me an error so how can i make it to read more than one line i tried a for loop but stil it only reads number 1 that is in between the brackets ...!!!
thank you
You can do it like this:
String lines[] = {"You are a friend:",
"name= [1]",
"From= [ 2011-02-28 07:00:52]",
"To= [2011-03-17 07:01:02]",
"Link= [http://www.example.com]",
"type= [good] for some"};
for (int i = 0; i < lines.length; i++)
{// your previous code, but use lines[i] instead of s }
If the brackets are always at the beginning and end it's probably easier to do something like this:
String s = addressString;
String innerText = s.substring(1,s.length-1);
Related
I am writing an app with Android Studio and I want to split a text into different values.
I have following text in result
*"Name: Peter;Age: 25; City: Chicago"*
I want to get:
*Name = Peter;
Age = 25;
City = Chicago;*
I used the search function and found these solutions: Android Split string but for my problem it seems to be too complicated.
The easiest way is to use split() method.
String s1="Name: Peter;Age: 25; City: Chicago";
String[] words=s1.split(";");
//using java foreach loop to print elements of string array
for(String w:words)
{
Log.i("Words: ", w);
}
I am building a quiz app with questions in a string resource, as well as an answer. They are formatted by numbers like so:
<string name="ques1">What color is the sky?</string>
<string name="ques2">What sound does a cow make?</string>
The answers are also strings corresponding to the same number as the questions:
<string name="ans1">Blue</string>
<string name="ans2">Moo</string>
I have created a QA class for holding both a question and the answer, as well as the user's response from an EditText. At the "loading" of my app these classes are created and filled by reading from the strings resource.
I can programmatically enter these no problem, it is a lot of copy pasting but it will get the job done:
QA.setQuestion(getString(R.string.ques1));
QA.setAnswer(getString(R.string.ans1));
quizList.add(QA);
QA.setQuestion(getString(R.string.ques2));
QA.setAnswer(getString(R.string.ans2));
quizList.add(QA);
etc...
The problem is that I want to be able to add questions and answers to the xml at any time without having to add yet another repetition of the above method. What I want to do is essentially this:
String refBase = "R.string."
String ans = "ans";
String ques = "ques";
int numOfQues = 25; //only change when questions are added or removed
for (int i = 0; i < numOfQues; i++)
{
String referenceQ = refBase + ques + i;
String referenceA = refBase + ans + i;
QA.setQuestion(getString(referenceQ));
QA.setAnswer(getString(referenceA));
quizList.add(QA);
}
I cannot cast a string to an int like this obviously, but I am wondering if there is a way to implement a reference "builder", where I don't have to repeat many lines of code just to read another string with the same name but incremented number.
I understand that I can also just create an array.xml with one for questions and one for answers, making sure their position in each array corresponds. This would be easiest I think, but I guess I am wondering if it is possible to create references to string values through the code like my example?
You can use this method to get the question or answer String by its resource name:
String getQAString(boolean isQuestion, int index) {
String prefix = isQuestion? "ques" : "ans";
int resId = getResources().getIdentifier(prefix + index, "string", getPackageName());
return resId != 0? getString(resId) : "";
}
The loop to add questions and answers (assume they start from 1 and end at 25):
int numOfQues = 25;
for (int i = 1; i <= numOfQues; i++) {
String referenceQ = getQAString(true, i);
String referenceA = getQAString(false, i);
QA.setQuestion(referenceQ);
QA.setAnswer(referenceA);
quizList.add(QA);
}
I was wondering how I could programmatically edit strings in android. I am displaying strings from my device to my website, and the apostrophes ruin the PHP output. so in order to fix this, I needed to add character breaks, ie: the backslash '\'.
For example, if I have this string: I love filiberto's!
I need android to edit it to: I love filiberto\'s!
However, each string is going to be different, and there will also be other characters that I have to escape from . How can I do this?
I was wondering how I could programmatically edit strings in android. I am displaying strings from my device to my website, and the apostrophes ruin the PHP output. so in order to fix this, I needed to add character breaks, ie: the backslash '\'.
This is what I have so far, thanks to ANJ for base code...:
if(title.contains("'")) {
int i;
int len = title.length();
char[] temp = new char[len + 1]; //plus one because gotta add new
int k = title.indexOf("'"); //location of apostrophe
for (i = 0; i < k; i++) { //all the letters before the apostrophe
temp[i] = title.charAt(i); //assign letters to array based on index
}
temp[k] = 'L'; // the L is for testing purposes
for (i = k+1; i == len; i++) { //all the letters after apostrophe, to end
temp[i] = title.charAt(i); //finish the original string, same array
}
title = temp.toString(); //output array to string (?)
Log.d("this is", title); //outputs gibberish
}
Which outputs random characters.. not even similar to my starting string. Does anyone know what could be causing this? For example, the string "Lol'ok" turns into >> "%5BC%4042ed0380"
I am assuming you are storing the string somewhere. Lets say the string is: str.
You can use a temporary array to add the '/'. For a single string:
int len = str.length();
char [] temp = new char[len+1]; //Temporary Array
int k = str.indexOf("'"), i; //Finding index of "'"
for(i=0; i<k-1; i++)
{
temp[i] = str.charAt(i); //Copying the string before '
}
temp[k] = '/'; //Placing "/" before '
for(i=k; j<len; j++)
{
temp[i+1] = str.charAt(i); //Copying rest of the string
}
String newstr = temp.toString(); //Converting array to string
You can use the same for multiple strings. Just make it as a function and call it whenever you want.
The String API has a number of API calls that could help, for example String.replaceAll. But...
apostrophes ruin the PHP output
Then fix the PHP code rather than require "clean" input. Best option would be to select a well supported transport format (say JSON or XML) and let the Json API on each end handle escape code.
I've this string:
1,Diego Maradona,Footballer,Argentina
I need manipulate and split this string for this output for set the textView:
1
Diego Maradona
Footballer
Argentina
I tried this method:
String phrase = "1,Diego Maradona,Footballer,Argentina";
String delims = "[,]";
String[] tokens = phrase.split(delims);
for (int i = 0; i < tokens.length; i++)
textView.setText(Html.fromHtml(tokens[i]));
But the output is last value of the string, why?:
Argentina
Because you're using setText() which replaces the previous content, so you're seeing just the last item. Use .append() instead, this way you'll be able to see all the entries.
I have the following String ressource:
<string name="foo">This is a {0} test. Hello {1}</string>
Now I want to pass the values 1 and foo when calling:
getResources().getText(R.string.foo)
how to make this? Or is it not possible?
getResources().getString(R.string.foo, 1, "foo"); but string should be using string format ... so your string in string should looks like:
<string name="foo">This is a %d test. Hello %s</string>
I'm not too sure if Java has something like this inbuilt. I did once write a method that would do the exact thing you're looking for, however:
public static String format(String str, Object... objects)
{
String tag = "";
for (int i = 0; i < objects.length; i++)
{
tag = "\\{" + i + "\\}";
str = str.replaceFirst(tag, objects[i].toString());
}
return str;
}
And this would format the string, to replace the '{i}' with the objects passed; just like in C#.
example:
format(getResources().getString(R.string.foo), "cool", "world!");
You can do it this way :
string name="foo">This is a %d test. Hello %s string>
with
getString(R.string.foo, 1, "foo");
Source : Are parameters in strings.xml possible?
You can find more information on formatting and formats here : http://developer.android.com/reference/java/util/Formatter.html
I believe that the simplest way to do what you're wanting is to save the line of code you have to a variable then use the Java String replace() function.
String fooText = getResources().getText(R.string.foo);
fooText = fooText.replace("{0}", myZeroVar);
fooText = fooText.replace("{1}", myOneVar);