How to remove String null in android - android

I want to remove String null and want to show like this Item 1, Item2, Item 3 Here is output... nullItem 1Item 2Item 3
String result;
for (Cuisine c : boxAdapter.getBox()) {
if (c.checkbox) {
result += c.name;
}
}
// result = result.substring(0,4);

Initialize your String like this:
String result = "";
instead of just:
String result;

You can do it like this, put a null check.
String result;
for (Cuisine c : boxAdapter.getBox()) {
if (c.checkbox) {
if(c.name != null)
result += c.name;
}
}
OR
You can just initialize your String with empty string like:-
String result = "";

You're appending the three box names to an initially null String (result) , solve this problem by making your String an empty string at the beginning :
String result = "";

Find the solution
String result;
for (Cuisine c : boxAdapter.getBox()) {
if ((c.name!=null)&&(c.checkbox)) {
result += c.name;
}
}
//

Use regular expression to replace non-printable characters.
String string=line.replaceAll("[^\\p{Print}]","");

Related

How to get the searched text in a string?

I want to get a text that it is a part of an string.For example: I have a string like "I am Vahid" and I want to get everything that it's after "am".
The result will be "Vahid"
How can I do it?
Try:
Example1
String[] separated = CurrentString.split("am");
separated[0]; // I am
separated[1]; // Vahid
Example2
StringTokenizer tokens = new StringTokenizer(CurrentString, "am");
String first = tokens.nextToken();// I am
String second = tokens.nextToken(); //Vahid
Try:
String text = "I am Vahid";
String after = "am";
int index = text.indexOf(after);
String result = "";
if(index != -1){
result = text.substring(index + after.length());
}
System.out.print(result);
Just use like this, call the method with your string.
public String trimString(String stringyouwanttoTrim)
{
if(stringyouwanttoTrim.contains("I am")
{
return stringyouwanttoTrim.split("I am")[1].trim();
}
else
{
return stringyouwanttoTrim;
}
}
If you prefer to split your sentence by blank space you could do like this :
String [] myStringElements = "I am Vahid".split(" ");
System.out.println("your name is " + myStringElements[myStringElements.length - 1]);

I am trying to validate my mobile number(whic I picked up form my contact list) in my registration page but it is picking up space in between it

I am trying to validate my mobile number which I am picking up from Contac List but the problem is that it is picking contact like +91 95 xx xxxxxx and there is space between numbers so the validation is not happening.
For now I am using this validation
String Mobile = "^[+]?[01]?[- .]?(\\([2-]\\d{2}\\)|[2-9]\\d{2})[- .]?\\d{3}[- .]?\\d{4}$";
But it's a failure.
Any help is appreciable.
Thank you.
In android sdk One class is there PhoneNumberUtils by using that class you can validate your phone number:
check the documentation link here
Replace the spaces then validate..
String phone=phone.replace(" ","");
Now the String phone has the text without spaces and you can compare.
Just try this.
String number = " " //Your mobile number
number.replaceAll("[^+\\d]|(?<=.)\\+", ""));
This will remove all the spaces, (, ) symbols
you can use below code :
phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
try
{
if (phoneNumber != null)
phoneNumber = extractDigits(phoneNumber);
}
catch (Exception e)
{
e.printStackTrace();
}
where extractDigits() method is as :
public static String extractDigits(String src)
{
StringBuilder builder = new StringBuilder();
String firstchar = src.substring(0, 1);
for (int i = 0; i < src.length(); i++)
{
char c = src.charAt(i);
if (Character.isDigit(c) || firstchar.equals("+"))
{
firstchar = "";
builder.append(c);
}
}
return Checkvalidnumber(builder.toString());
}
where Checkvalidnumber() method is as :
public static String Checkvalidnumber(String number)
{
String validnumber = "";
String Countrycode = chatPrefs.getString("countryCode", "");
if (number.startsWith("+"))
{
validnumber = number.trim();
}
else if (number.startsWith("0"))
{
validnumber = (Countrycode + number.substring(1, number.length())).trim();
}
else
{
validnumber = (Countrycode + number).trim();
}
return validnumber;
}
You should try the following to remove the white spaces and then you can do the validation.
var str = "PB 10 CV 2662";
str = str.replace(/ +/g, "");

Deleting a word in a String/String Array

So if you are adding in a string, you can just add them via += method(the one i know and using atm). but how can you delete a word in a string/string array?
example: i have a string
String="Monday,Tuesday,Wednesday"
how do you make it into
String="Monday,Wednesday"
any help please?
You could use the replace method.
String sentence = "Monday,Tuesday,Wednesday";
String replaced = sentence.replace("Tuesday,", "");
its easy
just use
yourString = yourString.replaceAll("the text to replace", ""); //the second "" show empty string so the text will get replace by empty string
finally yourString will contain the text u desire Thats it :)
You can use the "public String replace(char oldChar, char newChar)" method if you want to remove "Tuesday" and not the second element
https://stackoverflow.com/questions/16702357/how-to-replace-a-substring-of-a-string
I think, i will use it for simplicity otherwise go to other suggested answer...
Use Arraylist for storing days:
ArrayList<String> days = new ArrayList<String>();
days.add("Monday");
days.add("Tuesday");
days.add("Wednesday");
Use it for creating days string:
public String getDays() {
String daysString = "";
for (int i = 0; i < days.size(); i++) {
if (i != 0)
daysString += ", ";
daysString += days.get(i);
}
return daysString;
}
And whenever you want to remove use
days.remove(1);
or
days.remove("Tuesday");
then again call getDays();
IInd Method if you want to use only string:
String list = "Monday,Tuesday,Wednesday";
System.out.println("New String : " + removeAtIndex(list, 1));
and
public String removeAtIndex(String string, int index) {
int currentPointer = 0;
int lastPointer = string.indexOf(",");
while (index != 0) {
currentPointer = string.indexOf(',', currentPointer) + 1;
lastPointer = string.indexOf(',', lastPointer + 1);
index--;
}
String subString = string.substring(currentPointer,
lastPointer == -1 ? string.length() : lastPointer);
return string.replace((currentPointer != 0 ? "," : "") + subString
+ (currentPointer == 0 ? "," : ""), "");
}
Something like this using a regular expression:
String contents = "Monday,Tuesday,Wednesday";
contents = contents.replaceAll("[\\,]+Tuesday|^Tuesday[\\,]*", "");

How can I make this line shorter with arrays?

if (edittext.getText().toString().toLowerCase().contains("where") &
(edittext.getText().toString().toLowerCase().contains("you")) &
(edittext.getText().toString().toLowerCase().contains("from")))
I tried using an array for this by searching a string array with contains, but that doesn't work, just syntax error.
How can I make this line shorter by using arrays?
String[] array = {"where", "you", "from"};
String value = edittext.getText().toString().toLowerCase();
for (String word : array) {
if (value.contains(word)) {
//Do something
}
}
String lcText = edittext.getText().toString().toLowerCase();
if (lcText.contains("where") && lcText.contains("you") && lcText.contains("from")) {
// Do stuff...
}
String value = edittext.getText().toString().toLowerCase();
if (value.contains("where") & (value.contains("you") & value.contains("from"))

android function in class throws java.lang.NullPointerException

I've made a class which holds some string and integers, in that class I made a function to convert the data in the class in to a readable string;
public String GetConditions() {
String BigString = null;
String eol = System.getProperty("line.separator");
try {
BigString += "Depth: " + ci(Depth) + eol;
and so on...
Because I have to convert many integers, I made an extra function to convert a integer to a string;
public String ci(Integer i) {
// convert integer to string
if (i != null) {
String a = new Integer(i).toString();
return a;
} else {
return "n/a";
}
}
This throws a NullPointerException exception on return a. I'm quite new to Java, this is probally a noob question... Sorry about, thanks in advance!
There is a much simpler way to convert an Integer to a String: use String#valueOf(int).
public String ci(Integer i)
{
return i == null ? "n/a" : String.valueOf(i);
}
Try converting the Integer you pass in your method to string, instead of instantiating a new one.
You can do it straight forward like:
String a = i.toString();
or
String a = Integer.toString(i.intValue());
Thanks guys, but I found the problem, I've tried to add something to a string which was 'null' , this line:
String BigString = null;

Categories

Resources