Remove part of string??

Post your questions and help other users.

Moderator: Martin

Post Reply
Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Remove part of string??

Post by Lucy » 04 Dec 2019 11:51

Hey.. how can i trim, split, etc a string?
I have a gile that contains something similar to this: {text=Clear}
I want to remove the "{text=" as well as the end "}". I have tried:
-filename = "test.mp3";
name = left(filename, length(filename) - 4);
And tried with right command.
I have also tried putting the text in an object and using remove.
Ultimately i have no idea how to do it

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Remove part of string??

Post by Desmanto » 04 Dec 2019 17:33

Use replace().

Code: Select all

text = '{text=Clear}';
rep = replace(replace(text, '{text=', ""), '}', "");
However, once your data started to become more and more complex, using replace will be so painfully long.

Better method is to use regex.

Code: Select all

text = '{text=Clear}';
regex = findAll(text, '\\{text=(.*)\\}', true)[0][1];
{} are special char in regex, so have to be escaped with backslash. The backslash itself is also special char again Automagic scripting, have to escaped again, ends up double backslash to escape the curly braces.
(.*) to capture any text in between.
[0][1] to access the capture group 0 (first whole capture) and then the subgroup 1.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Lucy
Posts: 225
Joined: 10 Aug 2019 11:24

Re: Remove part of string??

Post by Lucy » 04 Dec 2019 18:05

Oh fantastic as usual darling... thankyou.

Post Reply