我想使用 Replaceall 方法替换从 flutter 中的文本字段获取的多个字符。我怎样才能正确实现它。\nIv\'e 尝试按如下方式执行此操作,但它不会替换字符。
\n\n_textSelect(String str){\n str=str.replaceAll(\'e\', \'\xc3\xa9\');\n str=str.replaceAll(\'i\', \'I\');\n str=str.replaceAll(\'b\', \'*\');\n str=str.replaceAll(\'v\', \'<\');\n\n return str;\n }\nRun Code Online (Sandbox Code Playgroud)\n\n语境
\n\nWidget build(BuildContext context) {\n return Scaffold(\n appBar: AppBar(\n title: Text(\'Retrieve Text Input\'),\n ),\n body: Padding(\n padding: const EdgeInsets.all(16.0),\n child: TextField(\n controller: myController,\n ),\n ),\n floatingActionButton: FloatingActionButton(\n // When the user presses the button, show an alert dialog containing\n // the text that the user has entered into the text field.\n onPressed: () {\n _textSelect(myController.text);//update\n return showDialog(\n context: context,\n builder: (context) {\n return AlertDialog(\n // Retrieve the text the that user has entered by using the\n // TextEditingController.\n content: Text(\n str,\n style:TextStyle(\n fontSize:50,\n fontFamily:"Italy"),),\n );\n },\n );\n },\n tooltip: \'Show me the value!\',\n child: Icon(Icons.text_fields),\n ),\n );\n }\nRun Code Online (Sandbox Code Playgroud)\n
我不擅长RegExp,所以也许有更好的方法。
String _textSelect(String str) {\n str = str.replaceAll('e', '\xc3\xa9');\n str = str.replaceAll('i', 'I');\n str = str.replaceAll('b', '*');\n str = str.replaceAll('v', '<');\n return str;\n}\n\n\nString output = _textSelect('English is blowing vhistle?'); \nRun Code Online (Sandbox Code Playgroud)\n\n编辑:
\n\nfinal myController = TextEditingController();\nString str = '';\n\nString _textSelect(String str) {\n str = str.replaceAll('e', '\xc3\xa9');\n str = str.replaceAll('i', 'I');\n str = str.replaceAll('b', '*');\n str = str.replaceAll('v', '<');\n return str;\n}\n\n@override\nWidget build(BuildContext context) {\n return Scaffold(\n appBar: AppBar(\n title: Text('Retrieve Text Input'),\n ),\n body: Padding(\n padding: const EdgeInsets.all(16.0),\n child: TextField(\n controller: myController,\n ),\n ),\n floatingActionButton: FloatingActionButton(\n // When the user presses the button, show an alert dialog containing\n // the text that the user has entered into the text field.\n onPressed: () {\n str = _textSelect(myController.text); //update\n return showDialog(\n context: context,\n builder: (context) {\n return AlertDialog(\n // Retrieve the text the that user has entered by using the\n // TextEditingController.\n content: Text(\n str,\n style: TextStyle(fontSize: 50, fontFamily: 'Italy'),\n ),\n );\n },\n );\n },\n tooltip: 'Show me the value!',\n child: Icon(Icons.text_fields),\n ),\n );\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
10008 次 |
| 最近记录: |