Skip to content

Added a replace function to strings - #12782

Open
R-Cramer4 wants to merge 3 commits into
slint-ui:masterfrom
R-Cramer4:string-functions
Open

Added a replace function to strings#12782
R-Cramer4 wants to merge 3 commits into
slint-ui:masterfrom
R-Cramer4:string-functions

Conversation

@R-Cramer4

@R-Cramer4 R-Cramer4 commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Functions added

replace(from, to)

This replaces all occurrences of from with to and returns a new string. This makes it easy to modify parts of strings in slint without going to the backend language.

LineEdit {
    input-type: number;
    text: "100%";
    changed has-focus => {
        if (self.has-focus) {
            // Remove the % when focused
            self.text = self.text.replace("%", "");
        }
    }
    accepted => {
        // Add it back when the input is accepted
        self.text = self.text + "%";
    }
}

@tronical

tronical commented Aug 4, 2026

Copy link
Copy Markdown
Member

I'd like to better understand the use-cases for index based string operations. I'm all in favor of safe iterators of kinds, but even indexing by grapheme cluster can destroy strings in a UI, and that's bad. For example, in Indic scripts you can split at grapheme cluster boundaries, but the resulting string may be an incomplete syllable, which may be unreadable to the user without context. The same goes for Arabic, where you may end up with valid Unicode, but different shaping because of missing adjacent clusters.

Given that complex scripts are the least tested in real life apps ("oh, here is the machine translation, I guess it's fine") I'd like to avoid that APIs that work fine with western scripts (predominantly used by devs/designers) will break with end users.

By better understanding the use-cases, perhaps we can design safer APIs.

@R-Cramer4

Copy link
Copy Markdown
Contributor Author

The main use case is rapid simple prototyping of user interaction patterns without going to the backend language. With this use case I think there are two directions we can go in. The first is simply putting index based string operations behind experimental features so it can be used when prototyping and then can be dropped later. The second is making substring work on words and not grapheme clusters which can be done with the existing unicode_segmentation crate. I would need to think more about implementing at() if we went in this direction though.

@R-Cramer4 R-Cramer4 changed the title Added basic string manipulation Added a replace function to strings Aug 5, 2026
@R-Cramer4

Copy link
Copy Markdown
Contributor Author

Dropped at and substring due to there being no correct way to index properly. If these functions are needed, they should be delegated to a backend language like python or javascript.

@tronical tronical left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a string replacement function is good to have.

Comment thread internal/core/string.rs Outdated
Comment thread docs/astro/src/content/docs/reference/property-types/strings.mdx Outdated
Comment thread api/cpp/include/private/slint_string.h Outdated
Comment thread internal/interpreter/eval.rs Outdated
Comment thread internal/interpreter/eval.rs
Comment thread internal/compiler/generator/rust.rs Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants