Sort of a String#replace for working with Components
See original GitHub issueI’m looking for a way to take a config value like %displayname%: %message%
and replace %displayname% and %message% with components (and somehow probably also parse & color codes at the same time (ex. %displayname%&r: %message%
))
Example: (simplified for demonstration purposes, in the real world the replaced components could have click & hover events and wouldn’t just be a simple text component with just content)
Component comp = TextComponent.of("%displayname%: %message%")
.replace("%displayname", TextComponent.of("Steve"))
.replace("%message%", TextComponent.of("Hello World!"));
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Sort of a String#replace for working with Components #38
I'm looking for a way to take a config value like %displayname%: %message% and replace %displayname% and %message% with components (and somehow probably ......
Read more >Safe/Efficient to use string.replace() with styled components?
To clarify, I do have it working: const replaceStringWithTheme = (string, theme) => { const modifiedString = string .replace("primary", ...
Read more >String.Replace Method (System)
Returns a new string in which all occurrences of a specified Unicode character or String in the current string are replaced with another...
Read more >Java String replace() method
The Java String class replace() method returns a string replacing all the old char or CharSequence to new char or CharSequence. Since JDK...
Read more >Python String | replace()
The replace() in Python returns a copy of the string where all occurrences of a substring are replaced with another substring. Syntax of...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
https://github.com/KyoriPowered/text/blob/master/api/src/test/java/net/kyori/text/TranslatableComponentTest.java is as about as good of an example I can find for this. (thanks for the pointer, @wizjany)
I’m not really a fan of using the argument index to do the substitution though… I can already see users messing it all up seeing as these messages will be user-configured. Is there anything comparable to this but with named arguments or will this need to be done by the implementing library? I feel like this would be a useful addition if there isn’t something already.
Use a translatable component? then you can use the friendly renderer and provider a translation function that replaces placeholders with values.