如何从颤动中的选定文本字段值中删除特定字母
How to remove specific letters from a selected textfield value in flutter
所以我有这个带有控制器的文本字段,我能够获得突出显示的选定值并毫无问题地更改它。
CustomTextField(
hintText: "Write something...",
maxLines: null,
controller: _controller,
addBorder: false,
),
//Lets say the text inside the textfield is "Good morning my friends"
CustomButton(
text: "Change",
onClick: () {
//This is to get the highlighted/selected text inside the textfield (Lets say it's "morning")
String highlightedText = _controller.selection.textInside(_controller.text);
//This is to get all the text before the highlighted text = "Good "
String textBefore = _controller.selection.textBefore(_controller.text);
//This is to get all the text after the highlighted text = " my friends"
String textAfter = _controller.selection.textAfter(_controller.text);
//This is to change or modify the highlighted text = "**morning**"
String changeSelectedText = "**$selectedText**";
//This is to join the before and after text and add to back to the controller = "Good **morning** my friends"
String newText = textBefore + changeSelectedText + textAfter;
print(newText);
_controller.text = newText;
}),
现在我的问题是如何首先检查“morning”是否已经被“**morning**”包围。因为我用过这个
if (textAfter.contains("**") || textBefore.contains("**")) {
textAfter = textAfter.replaceAll("**", "");
textBefore = textBefore.replaceAll("**", "");
}
但它会检查文本字段中的每个词是否 ** 不是我选择的词。所以请问我该怎么做,比如正则表达式或使用它 positions
改用endsWith
和startsWith
:
if (textAfter.startsWith("**") || textBefore.endsWith("**")) {
// Don't replace all ** with empty text, just make a substring and cut them off
// textAfter = textAfter.replaceAll("**", "");
// textBefore = textBefore.replaceAll("**", "");
textAfter = textAfter.substring(2);
textBefore = textBefore.replaceAll(0, textBefore.length - 2);
}
所以我有这个带有控制器的文本字段,我能够获得突出显示的选定值并毫无问题地更改它。
CustomTextField(
hintText: "Write something...",
maxLines: null,
controller: _controller,
addBorder: false,
),
//Lets say the text inside the textfield is "Good morning my friends"
CustomButton(
text: "Change",
onClick: () {
//This is to get the highlighted/selected text inside the textfield (Lets say it's "morning")
String highlightedText = _controller.selection.textInside(_controller.text);
//This is to get all the text before the highlighted text = "Good "
String textBefore = _controller.selection.textBefore(_controller.text);
//This is to get all the text after the highlighted text = " my friends"
String textAfter = _controller.selection.textAfter(_controller.text);
//This is to change or modify the highlighted text = "**morning**"
String changeSelectedText = "**$selectedText**";
//This is to join the before and after text and add to back to the controller = "Good **morning** my friends"
String newText = textBefore + changeSelectedText + textAfter;
print(newText);
_controller.text = newText;
}),
现在我的问题是如何首先检查“morning”是否已经被“**morning**”包围。因为我用过这个
if (textAfter.contains("**") || textBefore.contains("**")) {
textAfter = textAfter.replaceAll("**", "");
textBefore = textBefore.replaceAll("**", "");
}
但它会检查文本字段中的每个词是否 ** 不是我选择的词。所以请问我该怎么做,比如正则表达式或使用它 positions
改用endsWith
和startsWith
:
if (textAfter.startsWith("**") || textBefore.endsWith("**")) {
// Don't replace all ** with empty text, just make a substring and cut them off
// textAfter = textAfter.replaceAll("**", "");
// textBefore = textBefore.replaceAll("**", "");
textAfter = textAfter.substring(2);
textBefore = textBefore.replaceAll(0, textBefore.length - 2);
}