Split string into two separate variables - android

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.

Related

Spliting a Text on multiple conditions

I am writing an app with Android Studio and I want to split a text into different values.
I have following text in result
*"Name: Peter;Age: 25; City: Chicago"*
I want to get:
*Name = Peter;
Age = 25;
City = Chicago;*
I used the search function and found these solutions: Android Split string but for my problem it seems to be too complicated.
The easiest way is to use split() method.
String s1="Name: Peter;Age: 25; City: Chicago";
String[] words=s1.split(";");
//using java foreach loop to print elements of string array
for(String w:words)
{
Log.i("Words: ", w);
}

Is there any method to add some string to what found by regex in Android Studio?(ctrl+shift+R)

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

String input distributed to array (Android)?

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").

Removing space from Edit Text String

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.

Android eclipse random string array for setText?

If I understand correctly
Random ran = new Random();
String[] ButtonText = null;
Resources res = getResources();
ButtonText = res.getStringArray(R.array.ButtonText_array);
String strRandom = ButtonText[ran.nextInt(ButtonText.length)];
System.out.println("Random string is : "+strRandom);
Is a way to take my string-array items and put them in random order and now I'm wanting to setText of several buttons with individual items from the strRandom. The following is for the setText of a button
Button gm1 = (Button) findViewById(R.id.gm1);
gm1.setText();
But I dont know how to put in the strRandom items into the setText part and since I dont need it displaying what do I need to alter here.
System.out.println("Random string is : "+strRandom);
I really am not understanding the question...
If you're just asking how to set the text to a random string, do it just as you did with the println() statement,
gm1.setText(strRandom);
or
gm1.setText(ButtonText[ran.nextInt(ButtonText.length)]);
Just a side note: by convention, variables are done in camelCase, reserve AllCaps for class names. (e.g. ButtonText should be buttonText). You'll notice the SO formatter formats ButtonText as if it were a class, not an array.
gm1.setText((CharSequence)("Random string is : " + strRandom));
You need to cast from String to CharSequence

Categories

Resources