In my android app, I am getting the String from an Edit Text and using it as a parameter to call a web service and fetch JSON data.
Now, the method I use for getting the String value from Edit Text is like this :
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
Now normally it works fine, but if we the text in Edit Text contains space then my app crashes.
for eg. - if someone types "food" in the Edit Text Box, then it's OK
but if somebody types "Indian food" it crashes.
How to remove spaces and get just the String ?
Isn't that just Java?
String k = edittext.getText().toString().replace(" ", "");
try this...
final EditText edittext = (EditText) findViewById(R.id.search);
String k = edittext.getText().toString();
String newData = k.replaceAll(" ", "%20");
and use "newData"
String email=recEmail.getText().toString().trim();
String password=recPassword.getText().toString().trim();
In the future, I highly recommend checking the Java String methods in the API. It's a lifeline to getting the most out of your Java environment.
You can easily remove all white spaces using something like this. But you'll face another serious problem if you just do that. For example if you have input
String input1 = "aa bb cc"; // output aabbcc
String input2 = "a abbcc"; // output aabbcc
String input3 = "aabb cc"; // output aabbcc
One solution will be to fix your application to accept white spaces in input string or use some other literal to replace the white spaces. If you are using only alphanumeric values you do something like this
String input1 = "aa bb cc"; // aa_bb_cc
String input2 = "a abbcc"; //a_abbcc
String input3 = "aabb cc"; //aabb_cc
And after all if you are don' caring about the loose of information you can use any approach you want.
Related
I am uploading the text to server, i just want to upload those string in html format
example
input:
Do you know the relation between two eyes...???
They never see each other... BUT
They blink together.
They move together.
They cry together.
They see together.
They sleep together.
They share a very deep bonded relationship...
However, when they see a pretty woman, one will blink and another will not...
sendtext = adding_textjoke.getText().toString();
//String htmlString = Html.toHtml(sendtext);
String str = "(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>?«»“”‘’]))";
Pattern patt = Pattern.compile(str);
Matcher matcher = patt.matcher(sendtext);
sendtext = matcher.replaceAll("$1");
System.out.println(sendtext);
Log.e("sendtext", sendtext);
new AddJokesTask().execute(sendtext);
How to do this in android?
You can do it like this
SpannableString contentText = (SpannableString) contentView.getText();
String htmlEncodedString = Html.toHtml(contentText)
SpannableStringBuilder text = (SpannableStringBuilder) contentView.getText();
String htmlEncodedString = Html.toHtml(text);
I need to receive user's input from an edittext, then add some elements inside it?
how can I do it programmatically?
let me explain:
user enters this phrase : ( Hello World )
I want my app change it to : ( Hoelplom wwoerlldc) for example
Any suggestion?
get the text from your edittext
String userInput = myEditText.getText().toString();
and then modify it as you want
To 'Modify' the String you can do it in many ways
You can use substrings , replace() , ... etc
also you can convert it to char array and modify it , then build your string again
String userInput = myEditText.getText().toString();
char[] myArray = userInput.toCharArray();
// your modification(s) here
userInput = new String(myArray);
In my android application I am using spinner for selecting data.. and I created string array for strings that to be displayed in spinner. I put all the details in strings folder. I wanted the selected text t be displayed in edit text once the user selected item..
For example : spinner is used to select country codes suppose user selected USA
then the selected text will be like this
United States of America,+001
I don't need t take all the text and display it in edit text. I need only the text after comma, that is +001. So is there any way to get the text after the comma only
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
I know this will display all text I want only text that dislpaying after comma
You can split your text on the comma:
String text = spinner.getSelectedItem().toString();
String[] splited_text = text.split(",");
text = splited_text[1];
Suppose the text is in a string name text. use this:
String[] temp = text.split(",")
String code = temp[1]; //+001 the code after , temp[0] contains the rest
String seperated[] = spinner.getSelectedItem().toString().split(",");
text = seperated[1];
This will return only "+001".
String code = text.substring(text.indexOf(','));
This is how to get string after last comma:
String founded;
Pattern p = Pattern.compile(".*,\\s*(.*)");
String dd = (String) "Your string, really, really2";
Matcher m = p.matcher(dd);
if (m.find()) {
founded = m.group(1); //returns "really2"
}
Basically, I need to know how to insert a user inputted string into a text edit on my gui from within my code.
Try this-
String str = "this is a text";
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText(str, TextView.BufferType.EDITABLE);
You can use any string in String object.
I need to separate the input of an Edit Text on android, the input is in this format 4589, so I want to send the 45 to a list view, and the 89 to a Edit Text, somebody can help me I will appreciate it. thanks
The question is not clear. But you can try something like this
EditText et = (EditText) findViewById(R.id.editText);
String input = et.getText().toString();
String toEditText = input.substring(0,2); //45
String toListView = input.substring(2); //89
now you have the strings, use setText() to print
int a = Integer.parseInt(editText.getText());
int listNum = a / 100; //45
int editNum = a - listNum*100; //89
Here is a solution using integer division.