After split array gives only first character - android

I have a value in the database table that I would like to split by | the value is Company|company.pdf|images.pdf. When I run the following code, it only returns C in the Debug.
What am I doing wrong?
String cname = null;
String cpdf = null;
String cimages = null;
String catGetGeneralNameSQL = "SELECT * FROM categorymeta WHERE key = 'category_general' AND company = " + dashboard_company_id;
Cursor catGetGeneralNameQuery = myDB.rawQuery(catGetGeneralNameSQL, null);
while(catGetGeneralNameQuery.moveToNext()){
String name = catGetGeneralNameQuery.getString(catGetGeneralNameQuery.getColumnIndex("value"));
String[] separated = name.split("|");
Log.d("LOG", separated[1]);
}

It's because the split() method take a regular expression as parameter, and | is a reserved character in regexp.
Try with : \\| to escape it.
public static void main(String[] args) {
String test = "Company|company.pdf|images.pdf";
String[] result = test.split("\\|");
System.out.println(result[1]);
}
It outputs :
company.pdf
See the split() method documentation : https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
The pipe (without being escaped) is used in regular expressions to separate alternate matching patterns, eg: aaa|bbb will match "aaa" or "bbb", that's why it's matching every single character when not escaped.
Hope it helps.

You need to escape the original special character:
String[] separated = name.split("\\|");

use:
String[] b = a.split("\\|");
it will work fine with this

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)
}

get specific characters of a String

I need to extract specfic information of a String. So I have to create two new Strings with the necessary information isolated.
The structure of the String: {line1=necessary information 1, line2=necessary information 2}
As you can see, I need the String values (necessary information 1: after '=' and before ',' and necessary information 2)
This is the String:
String telefonname_nummer = listview.getItemAtPosition((int) position).toString();
Thanks a lot.
You can try something like this:
String yourInfo = "line1=necessary information 1, line2=necessary information 2";
String[] parts = yourInfo.split(",");
String info1 = parts[0].split("=")[1];
String info2 = parts[1].split("=")[1];
try this way may help you
String urString = "line1=necessary information 1, line2=necessary information 2";
// First split your string with ","
String[] splitedString = urString.split(",");
//as u mention in Qestion u want string after "="
//so,
String firstString = splitedString [0].split("=")[1];
String scndString = splitedString [1].split("=")[1];
Log.e("firstString ",firstString );
Log.e("scndString",scndString);

I want to extract strings from data [duplicate]

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"

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