How do I remove comma from strings for android? - android

What I have : text1,text2,text3
What I want : text1 text2 text3
replace comma with space?

final String s = "text1,text2,text3".replace(",", " ");
I tried with both replace and replaceAll. But didn't work
This is because both replace() and replaceAll() don't change the String object, they return you a new one. Strings are immutable in Java.

Try This Way:
String data = "text1,text2,text3";
String temp = data.replace(","," ");
Now You have all

This what you should do
String str = "text1,text2,text3"
str = str.replace(","," ");

Related

Best way to put variables in a Textview?

I try to put one or more variables in a TextView.
For exemple :
Hello I am a "girl" and I live in "Boston"
I would like to know what is the best way to do it :
Can i do it directly in the layout file ?
Can i do it only via Java Class ?
Can i do it via values/styles.xml ?
For now i do it like this :
String text1 = "Hello I am a ";
String text2 =" and I live in ";
String var1= preferences.getString("sex", "null");
String var2= preferences.getString("city", "null");
TextView Text = (TextView) findViewById(R.id.text);
Text.setText(text1 + var1 + text2 + var2);
It works yes but in fact my TextView are very long so I don't think my way is really appropriate.
Can i have some advice ?
Use String.format(String format, Object... args)
String sex = preferences.getString("sex", "null");
String city = preferences.getString("city", "null");
String str = String.format("Hello I am a %s and I live in %s", sex, city);
TextView text = (TextView) findViewById(R.id.text);
text.setText(str);
Note - Avoid concatenation in TextView.setText()
If Text-view is really long then you should be try this.
String var1= preferences.getString("sex", "null");
String var2= preferences.getString("city", "null");
Text.setText("Hello I am a"+var1+"\n"+"I live in"+var2);
This is good way Present Text in Text View

Removal of comma in the end of a string android

i have problem how to , remove the comma in the of my output. I use replaceall but it doesnt remove the comma , this my code
public void onClick(View v) {
String space = "";
String foo = " ";
String foo1 = ",";
String sentences = null;
//Splitting the sentence into words
sentences = multiple.getText().toString().toLowerCase();
String[] splitwords = sentences.trim().split("\\s+");
for (String biyak : splitwords) {
foo = (foo + "'" + biyak + "'" + foo1);
foo.replaceAll(",$", " ");//foo.replaceAll();
wordtv.setText(foo);
My codes Output : 'Hello','world',
My desire output: 'Hello','world'
String instances are immutable. As a result, methods like replaceAll() do not modify the original string but instead return a modified copy of the string. So replace foo.replaceAll(...) with foo = foo.replaceAll(...).
you can use substring method of String class. or You can use deleteCharAt() method of StringBuilder or StringBuffer classStringBuffer sb=new StringBuffer(your_string);sb.deleteCharAt(sb.length()-1);
U can also use a if statement and traverse the whole string .
If a comma(,) is found replace it with a space(" ").

Add a "," in a string where in it there is this: "}{"

I'm working in a Android app and i need to put a "," between "}{" in my jsonstring before i can work with String#split(). For example i have this:
{"field1":"A","field2":"abcbab","field3":5}
{"field1":"B","field2":"fakfef","field3":25}
{"field1":"A","field2":"faefe","field3":12}
how can i do that?
Try this :
String newJsonStr = yourJsonStr.replaceAll("\\}\\s*\\{", "},{");
String text = "To be or,not,to be, that is the question.";
String k=",{}";
String newText = text.replaceAll(",", k); // Modify the string text
System.out.println(newText);
try this it will solve your problem

How get the first letter of TextView in Android?

I have not found documentation on how I can get the first letter of a value in a TextView?
Very easy,
String strTextview = textView.getText().toString();
strTextView = strTextView.substring(0,1);
Alternatively you can try following way too
char firstCharacter = textView.getText().toString().charAt(0);
To get the first letter you'll have to make this call:
char firstCharacter = myTextView.getText().charAt(0);
Use the method from below. Provide the string from TextView as the parameter.
public String firstStringer(String s) {
String str= s.substring(0, Math.min(s.length(), 1));
return str;
}
You can use this
TextView tv = (TextView) findViewById(R.id.textview);
String frstLetter = tv.getText().substring(0, 1);
it is simple. To retrieve the Text from the TextView you have to use getText().toString();
String textViewContent = textViewInstance.getText().toString();
and the first letter textViewContent.charAt(0)
To fetch the content of the string from TextView:
String content = textView.getText().toString();
To fetch the first character
char first = content.charAt(0);
Try this
String value = text.getText().toString();
String firstTen = value.substring(0, 1);

how to validate cityname,pincode from one string?

Example: I have a EditText and I want to check the first word is the city name and second word is the pincode. These both words are separated by comma(,).
Hey try this if you don't want to use split. YOu need to get string into a variable from edittext and then use the following code for doing yourself able to validate :)
String str = "tim,52250";
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Do this way..
String content="Mehsana,384001";
String[] contentArray=content.split(",");
And you will get
contentArray[0]=Mehsana
contentArray[1]=384001
then you can validate each string content..
Use split() to get the things done.
Ex:
String s= "abc,123"
String s1[]=s.split(",");
String city=s1[0];
String pincode=s1[1];
Try this
String strInput = editText.getText().toString();
String strSplit [] = strInput.split(",");
System.out.println("CityName : " + strSplit[0]);
System.out.println("PinCode : " + strSplit[1]);
String data = "ali,524513"
String []array = data.split(",")
you can validate array[0] and array[1] :)
System.out.println("Name: "+array[0]+" code: "+array[1]);

Categories

Resources