I've been looking for answer regarding my question but I cannot find one or maybe I'm not just using the right terms when searching.
My question is, how can I distribute string entered in an editText to an array?
This is about my porter2 stemming project. I need to distribute the strings entered in the EditText field to an array so i can scan whether x in array[x] is vowel or not.
Ex.
String in EditText field = "dog".
Array should be:
array[0] = d
array[1] = o
array[2] = g
I'm sorry that i cannot give a code because I really don't have any idea how to code this one. Thank you so much everyone. :)
You can use use .split() with an empty ("") string input:
String text = yourEditText.getText().toString();
String[] letters = text.split(""); // Split by empty string to be in an array
// letters == { "", "d", "o", "g" }
// ^ Note that this has an empty string element at the front; that's just a byproduct of how split() works.
If you want a char array instead, it's much easier to use .toCharArray():
String text = yourEditText.getText().toString();
char[] letters = text.toCharArray();
// letters == { 'd', 'o', 'g' };
Personally I'd use the second one; letter == 'a' is a much faster operation than letter.equals("a").
Related
Basically, what I am trying to do is add double quote to the heads and tails of the numbers
String a = 1;
String b = 2;
String c = 3;
to
String a = "1";
String b = "2";
String c = "3";
So, I use [1-9] to find all numbers. Then, all of a sudden, it comes to me that I don't know how to get the values which regex found, like don't know what to set between double quotes.
Hence, I am wondering if it's possible.
You should use \d+ instead of [1-9] or at the very least [0-9]+ to include the 0
The reason why you need the + is because your regex would not find 10 or any digits that has more than 1 digit. You can reference the groups that you have found by using $1 (first group) $2 (second group) and so on. So you could do "$1" as your substitution and (\d+) as your search although you might want to use a better regex ie:
=\s*(\d)+;
replace to
= "$1";
See https://regex101.com/r/SaT6nK/1
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);
I have this code:
String a = "{action= some text, task= some text}, {action= some text2, task= some text2}";
String[] b = a.split("\\{action\\=|\\,|\\}|task\\=");
for( String z : b){
Log.e("eto", z);
}
How do I store the text after "action=" which is some text to String action? same with task?
Use StringTokenizer. Example included in link.
EDIT: You may need to use a couple of them, first one for comma separation than other for equals.
I've seen many people do similar to this in order to get the last word of a String:
String test = "This is a sentence";
String lastWord = test.substring(test.lastIndexOf(" ")+1);
I would like to do similar but get the last few words after the last int, it can't be hard coded as the number could be anything and the amount of words after the last int could also be unlimited. I'm wondering whether there is a simple way to do this as I want to avoid using Patterns and Matchers again due to using them earlier on in this method to receive a similar effect.
Thanks in advance.
I would like to get the last few words after the last int.... as the number could be anything and the amount of words after the last int could also be unlimited.
Here's a possible suggestion. Using Array#split
String str = "This is 1 and 2 and 3 some more words .... foo bar baz";
String[] parts = str.split("\\d+(?!.*\\d)\\s+");
And now parts[1] holds all words after the last number in the string.
some more words .... foo bar baz
What about this one:
String test = "a string with a large number 1312398741 and some words";
String[] parts = test.split();
for (int i = 1; i < parts.length; i++)
{
try
{
Integer.parseInt(parts[i])
}
catch (Exception e)
{
// this part is not a number, so lets go on...
continue;
}
// when parsing succeeds, the number was reached and continue has
// not been called. Everything behind 'i' is what you are looking for
// DO YOUR STUFF with parts[i+1] to parts[parts.length] here
}
What I'm trying to do is find a way I can take the word "camel" for example from a EditText field and make for instance c=2 a=1 m=4 e=5 l=3. Is there anyway I can pull the individual characters from a string and convert them to numbers?
I've tried using "split" to separate each character into an array but I can't figure out how to convert the letters into numbers
so I can do something like:
a=1
b=2
c=3
int temp = (int)(array[1]+array[2]+array[3]+etc...)
using the example of "camel" would equal 15
This is what I have so far:
String name = inputarea.getText().toString();
String[] array = name.split("");
for(int i =0; i < array.length ; i++)
The biggest problem I keep having is if I try to pull from the 7th position in the array and nothing is there. (camel only has 5 characters) then I get a nice big error.
Thank you for any help that can be provided.
Edit: I figured it out after a few hours of playing with it here is my working code:
String firstname = inputarea.getText().toString();
char[] array = firstname.toCharArray();
final char[] array2 = new char[15];
System.arraycopy(array, 0, array2, 0, array.length);
if (array2[0] == 'A' ) {
array2[0] = '1';
}
suggestion:
first, need define all letter, from a-z (A-Z), the ASCII code 'a' to 'z' is 97 to 122, if you want support the upper letter, you need add A-Z.
then, get the letter in the string, u can use this:
for(int i=0;i<string.length();i++){
int number = string.charAt(i);
}
when you get the number size, you can reduce to the base number('a' is 97), you will get the individual number
Does String.charAt() works for you?
As for converting to number, if the numbers are consecutive you can define a fixed string with all the characters you want to map and use String.indexOf(). If not, you can have a parallel array with ints or use a Map.