new line on comma within string - android

I have an EditText view which will allow the user to edit an address field. I want any text with a comma before it to be put on a new line so for example the following:
Some St., Some City, Some post code would be presented as:
Some St.,
Some City,
Some post code
Anyone know how I could do this?

Perhaps you could perform a String.replace() to replace all commas with ,\n
so,
String s = "Some St., Some City, Some post code"
s = s.replace(",",",\n");
You then might have to do something to remove the whitespace
Alternatively, to remove all whitespace:
String s = "Some St., Some City, Some post code";
String strings[] = s.split(",");
for(int i = 0; i < strings.length; i++){
strings[i] = strings[i].trim();
strings[i] += ",\n";
}
s = "";
for(int i = 0; i < strings.length; i++)
s += strings[i];

Related

How to show list of string in TextView on Android

I want show list of strings in TextView and I get this list from server.
List from json :
"stars": [
{
"name": "Elyes Gabel"
},
{
"name": "Katharine McPhee"
},
{
"name": "Robert Patrick"
}
]
I want show this names such as this sample :
Stars = Elyes Gabel, Katharine McPhee, Robert Patrick
I should setText from this TextView in Adapter.
With below code I can show name :
model.get(position).getStars().get(0).getName();
But just show me Elyes Gabel !!!
I want show me such as this :
Stars = Elyes Gabel, Katharine McPhee, Robert Patrick
How can I it? Please help me
Here is the correct answer you might be after,
Lets just say you have the above JSON and you have converted that in a String array.
So array will look something like below:
String stars[] = {Elyes Gabel, Katharine McPhee, Robert Patrick}
TextView textView = // initialise the textview here or however you do.
StringBuilder builder = new StringBuilder();
for (String star: stars) {
builder.append(star);
builder.append(", ");
}
textView.setText(builder.toString());
You will get the desired output...
You need to loop through all "Star" elements and build the string yourself. You should have something like this:
String concatenatedStarNames = "";
List<Star> stars = model.get(position).getStars(); // I assume the return value is a list of type "Star"!
for (int i = 0; i < stars.size(); i++) {
concatenatedStarNames += stars.get(i).getName();
if (i < stars.size() - 1) concatenatedStarNames += ", ";
}
And then you set the text of the text view to concatenatedStarNames.
You can build it yourself with a StringBuilder, something like:
final Collection<Star> stars = models.get(position).getStars();
final StringBuilder builder = new StringBuilder();
boolean first = true;
for (Star star : stars) {
final String name = star.getName();
if(first) {
first = false;
builder.append(name);
} else {
builder.append(", ").append(name);
}
}
final String allStarNames = builder.toString();
you can just do - (with your same logic of accessing stars)
String strNames;
for (int i=0; i<starsCount; i++){ //starsCount = No of stars in your JSON
strNames += model.get(position).getStars().get(i).getName();
if( i != starsCount-1)
strNames += ", ";
}
textViewVariable.setText(strNames);

Print String[] In textView on Android

I've a String[] and i'd like print all positions this String in textview
code:
for (int i = 0; i < something.length; i++) {
myMsg.setText(something[i]);
}
but in textview, only last position is being displayed.
because you are replacing your text in your textview at every iteration by calling setText.
use append instead. like this:
for (int i = 0; i < something.length; i++) {
myMsg.append(something[i]);
}
if you don't want to use append for any reason. You can use Concatenation Operator +.
myMsg = ""; // initialize String to to empty
for (int i =0 ; i< something.length ; i++){
myMsg+= something[i];
}
You can try this too, If you dont want to use loop.
myMsg.setText(Arrays.toString(something));
or without commas and brackets
myMsg.setText(Arrays.toString(something).replaceAll("[,\\[\\]]+", " "));
or without commas and brackets and removed trailing and leading whitespace
myMsg.setText(Arrays.toString(something).replaceAll("[,\\[\\]]+", " ").trim());
Hope it helps :)

format a string with fill places

I have setup an edittext box and set the maxlength to 10. When I copy the edittext to a string myTitles. I need the myTiles to be 10 chars long and not dependent on what is entered in the edittext box.
myTitles[0] = TitlesEdit.getText().toString();
The edittext was filled with ABCD so I need to add 6 spaces or placeholders after the ABCD. I have seen other post with str_pad and substr without success
myTitles[0] = str_pad(strTest, 0, 10);
myTitles[0] = substr(strTest,0, 10);
Try something like
public static String newString(String str) {
for (int i = str.length(); i <= 10; i++)
str += "*";
return str;
}
This will return a String with * replaced for the empty ones.
So, for eg, if your String is abcde, then on calling newString() as below
myTitles[0] = newString("abcde");
will return abcde***** as the output.
String s = new String("abcde");
for(int i=s.length();i<10;i++){
s = s.concat("-");
}
Then output your string s.
Thank you Lal, I use " " to fill and it worked fine. here is my new code.
String strTest = TitlesEdit.getText().toString();
for (int i = strTest.length(); i <= 10; i++) {
strTest += " ";
}
Log.d("TAG", "String" + strTest);
myTitles[intLinenumber] = strTest;

how to apply the url encoder for the usersName in android

Hi in the below code output of usersName is [user3, user1, user2] in this output after , symbol it's giving space.
For that How to apply the urlencoder for usersName.
Final output I want like this usersName=[user3,user1,user2]
java
public String CreateGroup(String groupname,String username,
ArrayList<FriendInfo> result) throws UnsupportedEncodingException {
List<String> usersName = new ArrayList<String>();
for (int i = 0; i < result.size(); i++) {
usersName.add(result.get(i).userName);
}
String params = "groupname="+ URLEncoder.encode(groupname,"UTF-8") +
"&username="+ URLEncoder.encode(this.username,"UTF-8") +
"&password="+ URLEncoder.encode(this.password,"UTF-8") +
"&friendUserName=" +usersName+
"&action=" + URLEncoder.encode("CreateGroup","UTF-8")+
"&";
Log.i("PARAMS", params);
return socketOperator.sendHttpRequest(params);
}
Since you're passing arguments through http you need to encode the spaces by replacing them with "+" or "%20".
Your for should look like this:
for (int i = 0; i < result.size(); i++) {
usersName.add(result.get(i).userName.replace(" ","+"));
}
If you remove the spaces like the other anwser sugested your usernames would have incorrect information like "JohnSmith" instead of "John Smith"
Finaly since you're using your list you should do this:
"&friendUserName=" +usersName.toString().replace(" ","+")
Try to change this:
usersName.add(result.get(i).userName);
to this:
usersName.add(result.get(i).userName.replaceAll("\\s",""));
This is under the assumption that your userName is the one that includes the whitespace and not the userNames List.

Extract array elements and create a String from them

I have got an array through Vector addittion like
[1!,2!,3!,4!]
I have to convert it into a string like
{1!2!3!4!}
..can you please tell me the name of few methods by which i can make it? Thanks all..
String getElement = null;
for(int j = 0;j<5;j++){
getElement = dynamicViewtagNames.elementAt(j);
}
I can get elements of this array like this..then I have to convert it into a string.
I'm not sure if I understand your question correctly, but if you just want to turn this:
[1!,2!,3!,4!]
into
{1!2!3!4!}
you can for example make use of the String.replace() or String.replaceAll() method.
String str = "[1!,2!,3!,4!]";
str = str.replace("[", "{");
str = str.replace("]", "}");
str = str.replace(",", "");
If [1!,2!,3!,4!] is a Vector containing the Strings you showed us above, you could do it like this using a StringBuffer:
// letz assume this vector has the following content: [1!,2!,3!,4!]
Vector<String> dynamicViewtagNames = new Vector<String>();
StringBuffer b = new StringBuffer();
b.append("{");
for(int i = 0; i < dynamicViewtagNames.size(); i++) {
b.append(dynamicViewtagNames.get(i))
}
b.append("}");
String mystring = b.toString();
Simple Solution
String names = names.replaceAll(",","");
names = names.replaceAll("[", "{");
names = names.replaceAll("]", "}");
Use this code,
StringBuilder str = new StringBuilder();
for (int i = 0; i<dynamicViewtagNames.length;i++){
str.append(dynamicViewtagNames[i])
}
str.toString();
or you can use:
Arrays.toString(dynamicViewtagNames);
Thanks

Categories

Resources