Fifcio
Fifcio

Reputation: 53

Flutter - How to delete text after last '/' in string

I need help transforming this:

/storage/emulated/0/Android/data/testapp/files/textToDelete

into this

/storage/emulated/0/Android/data/testapp/files/

note: I don't want to use static value substring

Upvotes: 2

Views: 1855

Answers (2)

Fifcio
Fifcio

Reputation: 53

Used

.substring(0, var.lastIndexOf('/'));

and that worked perfectly

Upvotes: 0

timilehinjegede
timilehinjegede

Reputation: 14033

// the string to be trimmed
var myString = '/storage/emulated/0/Android/data/testapp/files/textToDelete';

// substring the string using the last index of the character + 1
var trimmedString = myString.substring(0, myString.lastIndexOf('/') + 1);

// print the trimmed string
print(trimmedString);

OUTPUT: /storage/emulated/0/Android/data/testapp/files/

Upvotes: 4

Related Questions