Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string on the last occurrence of some character

I'm basically trying to split a string on the last period to capture the file extension. But sometimes the file doesn't have any extension, so I'm anticipating that.

But the problem is that some file names have periods before the end like so...

/mnt/sdcard/OG Ron C, Chopstars & Drake - Choppin Ain't The Same-2013-MIXFIEND/02 Drake - Connect (Feat. Fat Pat) (Chopped Not Slopped).mp3 

So when that string comes up it chops it at "02 Drake - Connect (Feat."

This is what I've been using...

String filePath = intent.getStringExtra(ARG_FILE_PATH); String fileType = filePath.substring(filePath.length() - 4); String FileExt = null; try {     StringTokenizer tokens = new StringTokenizer(filePath, ".");     String first = tokens.nextToken();     FileExt = tokens.nextToken(); } catch(NoSuchElementException e) {     customToast("the scene you chose, has no extension :("); } System.out.println("EXT " + FileExt); File fileToUpload = new File(filePath); 

How do I split the string at the file extension but also be able to handle and alert when the file has no extension.

like image 866
sirvon Avatar asked Jan 03 '14 13:01

sirvon


People also ask

How do you split the last character of a string?

To split a string on the last occurrence of a substring:, use the lastIndexOf() method to get the last index of the substring and call the slice() method on the string to get the portions before and after the substring you want to split on.

How do you split a string on the first occurrence of certain characters?

To split a JavaScript string only on the first occurrence of a character, call the slice() method on the string, passing it the index of the character + 1 as a parameter. The slice method will return the portion of the string after the first occurrence of the character.

How do you split a string at a certain character?

To split a string with specific character as delimiter in Java, call split() method on the string object, and pass the specific character as argument to the split() method. The method returns a String Array with the splits as elements in the array.

How do you split a string at the last occurrence of a character with Python?

Python provides a method that split the string from rear end of the string. The inbuilt Python function rsplit() that split the string on the last occurrence of the delimiter. In rsplit() function 1 is passed with the argument so it breaks the string only taking one delimiter from last.


2 Answers

You can try this

int i = s.lastIndexOf(c); String[] a =  {s.substring(0, i), s.substring(i)}; 
like image 61
Evgeniy Dorofeev Avatar answered Sep 17 '22 07:09

Evgeniy Dorofeev


It might be easier to just assume that files which end with a dot followed by alphanumeric characters have extensions.

int p=filePath.lastIndexOf("."); String e=filePath.substring(p+1); if( p==-1 || !e.matches("\\w+") ){/* file has no extension */} else{ /* file has extension e */ } 

See the Java docs for regular expression patterns. Remember to escape the backslash because the pattern string needs the backslash.

like image 37
user1537366 Avatar answered Sep 21 '22 07:09

user1537366