Sub string

How can I get part of some string ?

For example :
string = “part1:3:CL:22:test”

I need just word “test”

Is it always the 5th part? Are they always separated by :
if so:

string.split(':')[4] 

Thanks

And How I can Capitalize the result ?

There is a capitalize formatter available.

yes, how do I apply the result in same string after split ?

Well, just apply the formatter using the formatters menu.

Yes, but how can I do this in the same place?wappler

You can click the magic wand to apply formatters. As you learn them, you can type them in code view. In this case just add
.capitalize()
To the end

In this case, ‘split’ could be used because the parts of the string were separated by a ‘:’. However, if the substring between ‘part1’ and ‘test’ was needed, is there way to do this in Wappler, using the formatters? Unless I’ve missed the relevant formatter, I think it would be necessary to create a custom formatter. I don’t think it’s one of the ‘hidden’ formatters either.

Could a simple formatter be added to the UI using ‘indexOf’?

'part1:3:CL:22:test'.split(':').slice(1, 4) => ['3','CL,'22']

Almost :slightly_smiling_face: The result is missing the colons. Easy to put back, if you know they’re colons. Not so easy if you need to extract any string, which you can’t necessarily chop up conveniently as in this example.

I think you demonstrate the point that some basic functions are missing - eg a function which returns the position of a string within a string. Javascript is not my strong point, but something like:

dmx.Formatters('string', {
    pos: function(str,srch) {
    return str.indexOf(srch);
  }
}); 

dmx.Formatters('string', {
    pos_i: function(str,srch) {
    return str.toLowerCase().indexOf(srch.toLowerCase());
  }
});

I spend a lot time parsing data. I wouldn’t want to do it without functions like this.

This regex will return the substring you are looking for.

(?<=:).+(?=:) => 3:CL:22

But still you need to know the string is delimited by colons. If not, how will you know where to start and end the match?

If you are sure that the content between the delimiter will always be alphanumeric you could build another regex that matches the first group between string and delimeter and last delimeter and string.

I was just making a general point that given Wappler has formatters like substring, it seems an omission that there isn’t a formatter to return the position of a string in a string - eg indexOf(). I would have thought extracting strings is a common task - eg getting the text between <h1> and </h1>.

2 Likes

Oh I see!

Thankfully they are implementing custom components with UI support for these type of things .

1 Like