【问题标题】:How to replace square root symbol in a string?如何替换字符串中的平方根符号?
【发布时间】:2013-07-27 06:09:51
【问题描述】:

在我正在使用的安卓应用中

String sqrt = "\u221a";
editText.setText(sqrt);

显示平方根符号。并且在该 editText 中输入的任何数学表达式都将被评估。当它们作为字符串传递时,我使用的数学库会评估有效的表达式。平方根必须是这样的:

sqrt(number)

所以我需要替换从编辑文本中得到的字符串中的平方根符号:

String input = editText.getText().toString();
input.replace(sqrt,"sqrt("); //doesn't work!

有什么办法可以做到吗?

【问题讨论】:

  • 也许您可以将字符串转换为 ascii 字符并从中删除 sqrt 的 ascii 代码。

标签: android string unicode character-encoding square-root


【解决方案1】:
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);

String sqrt = "\u221a";
editText1.setText(sqrt);

String input = editText1.getText().toString();
String new1 = input.replace(sqrt, "sqrt(");
editText1.setText(new1);

【讨论】:

    猜你喜欢
    • 2014-06-17
    • 2021-11-15
    • 2011-10-07
    • 1970-01-01
    • 2013-12-23
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    相关资源
    最近更新 更多