I want to extract strings from data [duplicate] - android

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 8 years ago.
I have data with repeating pattern '###'.I want to extract all strings between this pattern.
The data is like -
son###can###e###nick###54###
how can i get all data between the pattern '###'.

try this:
String patternString = "###";
Pattern pattern = Pattern.compile(patternString);
String[] split = pattern.split(text);

You can use Scanner with "###" delimiter.
Scanner in = new Scanner("son###can###e###nick###54###");
in.useDelimiter("###");
while(in.hasNext)
String x = in.next();
//Do something with x;
x will hold everything between ###. You can easily store them in an Array inside the loop or use them anyway you like.

Try this:
String str = "son###can###e###nick###54###";
String newStr = str.replaceAll("[#]+", "");
or you can separate all world via
String rem = "###";
Pattern pattern = Pattern.compile(rem);
String[] splitarr = pattern.split(text);
for(int i=0;i<aplitarr.length();i++)
{
String word=aplitarr[i].ToString();
}
Hope this may help you!

try,
String[] separated = CurrentString.split("###");
separated[0]; // this will contain "son"
separated[1]; // this will contain "can"
or
StringTokenizer tokens = new StringTokenizer(CurrentString, "###");
String first = tokens.nextToken();// this will contain "son"
String second = tokens.nextToken();// this will contain "can"

Related

How do I read only a specific part of the string in Android Studio?

If a send a string in the following format: 1234,1234,1234,1234; from Arduino to Android Studio (java (intelliJ)) based. with the amount of characters between every komma changing. How do i make it so that my code only reads the string from for example 0 to the , Or from the first , to the second ,?
If you are using Java, I would recommend looking into the Java.String.split() method. This method will split your string in an array of strings, depending on your delimiter. For example :
String s = "1234,1234,1234,1234";
String[] result = s.split(",");
You can split the input string based on any special character,in your case ,, as follows
String inputString = "1234,1234,1234,1234";
String[] separated = inputString.split(",");
Log.i("MainActivity",separated[0]) // prints the first string which is 1234
// to loop over all strings
for(String s : separated){
Log.i("MainActivity",s)
}

How do I convert string builder to string array in Android? [duplicate]

This question already has answers here:
StringBuilder to Array of Strings how to convert
(2 answers)
Closed 6 years ago.
I have a string builder in Android which is filled with data from a database, and I want to display that data using a list view. For that I want to covert string builder into an array of strings. Can somebody help me in this conversion, or suggest to me some other technique.
Here you can find Link
tempArray = sb.toString().split("ABCABC"); will split the string and return an array of strings for each line.
Try this :
String sbString = sb.ToString();
String[] ary = "abc".split("TABTAB");

How can i get few characters from String?

I want to retrieve few characters from string i.e., String data on the basis of first colon (:) used in string . The String data possibilities are,
String data = "smsto:....."
String data = "MECARD:....."
String data = "geo:....."
String data = "tel:....."
String data = "MATMSG:....."
I want to make a generic String lets say,
String type = "characters up to first colon"
So i do not have to create String type for every possibility and i can call intents according to the type
It looks like you want the scheme of a uri. You can use Uri.parse(data).getScheme(). This will return smsto, MECARD, geo, tel etc...
Check out the Developers site: http://developer.android.com/reference/android/net/Uri.html#getScheme()
Note: #Alessandro's method is probably more efficient. I just got that one off the top of my head.
You can use this to get characters up to first ':':
String[] parts = data.split(":");
String beforeColon = parts[0];
// do whatever with beforeColon
But I don't see what your purpose is, which would help giving you a better solution.
You should use the method indexOf - with that you can get the index of a certain char. Then you retrieve the substring starting from that index. For example:
int index = string.indexOf(':');
String substring = string.substring(index + 1);

check string with delimiter expected

I want to split string got from bluetooth. i'm using
StringTokenizer splitStr = new StringTokenizer(readMessage, "\\|");
String numberSpeed = splitStr.nextToken(); //splitStr[0].replaceAll("\\D+","");
String numberTorque = splitStr.nextToken(); //splitStr[1].replaceAll("\\D+","");
numberSpeed = numberSpeed.replaceAll("\\D+","");
numberTorque = numberTorque.replaceAll("\\D+","");
Did it with split string before.
If i get corupted data without delimiter the app crashes while trying to do impossible.
How to check if there is delimiter or not and then proceed split or skip it?
you can check for delimeter in string by contains() method
if(str.contains("_your_delimiter")) { //for safe side convert your delimeter and search string to lower case using method toLowerCase()
//do your work here
}
Try this, I use it in my app.
String container = numberSpeed ;
String content = "\\D+";
boolean containerContainsContent = StringUtils.containsIgnoreCase(container, content);
It will return true if it has delimiter, and false it not.
Use that with an if statement.
ex.
if(containerContainsContent){
//split it
} else {
//skip it
}
This is quote from tokenizer docs: StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code.
Try to user String.split() instead.
if(str.contains(DEILIMITER)) {
String tab[] = str.split(DEILIMITER);
//enter code here
}

Subtracting string from a string [Android]

I have following two string:
String one:
"abcabc/xyzxyz/12345/random_num_09/somthing_random.txt"
String Two:
"abcabc/xyzxyz/12345/"
What i want to do is attach path "random_num_09/somthing_random.txt" from string one two string two. So how can i subtract string two from string one and then attach remaining part to string two.
I have tried to do it by searching for the second last "/" in the string one and then doing sub string and attaching it to string two.
But is there any better way of doing it.
Thanks.
I think the best way is to use substrings, as you said:
String string_one = "abcabc/xyzxyz/12345/random_num_09/somthing_random.txt";
String string_two = "abcabc/xyzxyz/12345/";
String result = string_two + string_one.substring(string_one.indexOf(string_two)+1));
The other possibility is to use regex, but you would still be doing concatenation to get the result.
Pattern p = Pattern.compile(string_two+"(.*)");
Matcher m = p.matcher(string_one);
if (m.matches()) {
String result = string_two+m.group(1);
}
rather that a substring, replace is simpler to use:
String string1 = "abcabc/xyzxyz/12345/random_num_09/somthing_random.txt";
String string2 = "abcabc/xyzxyz/12345/";
String res = string2 + string1.replace(string2, "");

Categories

Resources