I have string:
Apple, Banana, Strawberry; Lemon, Watermelon; Orange
When I try this:
if(meaning.contains(";"))
{
meaning=meaning.replace(";", "\n");
}
Result:
Apple, Banana, Strawberry
Lemon, Watermelon
Orange
How to replace part of String one by one in order to replace ";" to "\n"+numStr?
1.Apple, Banana, Strawberry
2.Lemon, Watermelon
3.Orange
Didn't test it, but should work:
String[] lines = meaning.split(";");
StringBuilder res = new StringBuilder();
for (int i = 0, size = lines.length; i < size; i++) {
res.append(i + 1).append(". ").append(lines[i]).append("\n");
}
res.toString();
You can use the following code. The trick is to use String.split() instead of String.replace().
if(meaning.contains(";"))
{
StringBuilder builder = new StringBuilder();
String[] meanings = meaning.split(";");
for (int i = 0; i < meanings.length; i++) {
builder.append(String.format(Locale.US, "%d. %s\n", i, meanings[i].trim()));
}
Log.d("meanings", builder.toString());
}
The result will print:
1.Apple, Banana, Strawberry
2.Lemon, Watermelon
3.Orange
Related
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);
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;
String arrAnimal[] = {"cat","dog","parrot","fish"};
I need to shuffle each word and set it to TextView, when click button wanna go through each element(see Shuffeled elements).
Complete working code,
String[] animals = {"cat","dog","parrot","fish"};
for (int i = 0; i < animals.length; ++i) {
List<Character> letters = new ArrayList<>(animals[i].length());
for (char c : animals[i].toCharArray()) {
letters.add(c);
}
Collections.shuffle(letters);
StringBuilder builder = new StringBuilder();
for (char c : letters) {
builder.append(c);
}
animals[i] = builder.toString();
}
System.out.println(Arrays.toString(animals));
Outputs: [tca, ogd, roptar, sifh]
EDIT: To print line by line, change the last line to,
for (String s : animals) {
System.out.println(s);
}
Use Collections.shuffle( )
ArrayList<String> list = new ArrayList<String>();
list.add("cat");
list.add("dog");
list.add("parrot");
list.add("fish");
Collections.shuffle(list);
What is the validation expression for string(space)integer? I want to enter the data in the format of "month date"(eg.March 22) in database.
I think it'll help you
String abc = "March 2";
String[] split = abc.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < split.length; i++) {
sb.append(split[i]);
if (i != split.length - 1) {
sb.append(" ");
}
}
String combined = sb.toString();
At 0 position you'll get your months then get into a String and matches with your static array.
And at 1 position, you'll get your date, you can match it too.
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.