I want to ask. I got a string json response like this
"Bank Danamon|Reksa Dana Insight Money Syariah"
And i want to change that string to like this in Android setText
Bank Danamon
Reksa Dana Insight Money Syariah
This is the code when i set the response
txvSettlementName.setText(itemSettlement.getAccountName());
Is there a way to do that?
Thanks
You have to split the string like
String str = "Bank Danamon|Reksa Dana Insight Money Syariah";
String[] separated = str.split("|");
separated[0]; // this will contain "Bank Danamon"
separated[1]; // this will contain " Reksa Dana Insight Money Syariah"
String accountName = itemSettlement.getAccountName();
accountName = accountName.replace("|","\n");
txvSettlementName.setText(accountName);
This code will directly replace the character | with line break \n which you can directly set to TextView
Related
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...";
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!
I need to send encoded username and Password through request body.
Here I'm sending username and password in the format of JSON object.
How to do it?
Thanks in advance.
Try
Encode
String encodedUsername = URLEncoder.encode(username);
String encodedPassword = URLEncoder.encode(password);
Decode
String decodedUsername = URLDecoder.decode(encodedUsername);
String decodedPassword = URLDecoder.decode(encodedPassword);
A simple Google search would've done the trick...
URLEncoder should be the way to go. You only need to keep in mind to
encode only the individual query string parameter name and/or value,
not the entire URL, for sure not the query string parameter separator
character & nor the parameter name-value separator character =.
String q = "random word £500 bank $";
String url = "http://example.com/query?q=" + URLEncoder.encode(q, "UTF-8");
...
Source:
Java URL encoding of query string parameters
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);
I have a ArrayAdapter the places JSON strings into a list view. The problem is when it places the strings into the textview it leaves brackets and quotation marks. For example, if the string contained the name Bob. The string would show up in the ListView as ["Bob"]. How do I remove the brackets and quotation marks?
Here is what I use to get the JSON strings,
String username = json2.getString(KEY_USERNAME);
String number = json2.getString(KEY_NUMBER);
String content = json2.getString(KEY_COMMENT);
tempList2.add (new Item (username, number, content));
customAdapter.addAll(tempList2);
You can use a simple replace:
String bob = "[\"Bob\"]";
bob = bob.replace("[", "").replace("\"", "").replace("]", "");
Log.i("Test", bob);
This will now just print Bob
As an addon to Phil's answer, the end quote (in my chrome browser) did not get removed. I fixed it by using .replace("\"", "") again at the end.