C#:更改文本框中的数字

发布于 2024-12-01 01:12:04 字数 1436 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

又爬满兰若 2024-12-08 01:12:04

您是否考虑过简单的 string.Replace?即

myText = myText.Replace("1", "1,");

对每个数字重复

您还需要编辑文本框吗?在将文本发送到文本到语音实用程序之前,您不能只处理文本(从文本框中获取)吗?

Have you considered a simple string.Replace? i.e.

myText = myText.Replace("1", "1,");

Repeat for each number

Also do you need to edit the text box, can't you just process the text (taken from the text box) before sending it to the text to speech utility?

够运 2024-12-08 01:12:04

沿着空格和其他空格分割,然后您可以检查每个单词是否是数字。

然后,您可以将该单词转换为字符,添加逗号,然后将其放回原处。

理想情况下,如果您的文本转语音解决方案支持它,请将其复制回隐藏文本字段,然后将其用于语音,这样用户看不到修改。

为了简单起见,您可能只想使用 tryParse:

http://msdn.microsoft .com/en-us/library/f02979c7.aspx

bool result = Int32.TryParse(value, out number);

其中 value 是 String

更新

阅读问题的编辑后,通过拆分为单词,然后开始将字符串转换为小写,以便您可以轻松比较,然后循环遍历每个单词。

StringBuilder buf = new StringBuilder();
for(int t = 0; t < wordarray.length - 1; t++) {
  String s = wordarray[t];
  if (numberWord.contains(s) && isWordNumber(wordarray[t + 1]) {
      s = addCommas(wordarray[t + 1]);
  }
  buf.append(' ').append(s);
}

Split along spaces and other whitespace, then you can check if each word is a number.

Then you can convert that word into characters, add in commas, then put it back in.

Ideally, if your text-to-speech solution supports it, copy this back into a hidden text field, then use that for the speech, so the user doesn't see the modification.

You may want to just use tryParse though, for simplicity:

http://msdn.microsoft.com/en-us/library/f02979c7.aspx

bool result = Int32.TryParse(value, out number);

Where value is a String.

Update

After reading the edit to the question, by splitting into words, then start with converting the string to lower-case, so you can easily compare, then, just loop through each word.

StringBuilder buf = new StringBuilder();
for(int t = 0; t < wordarray.length - 1; t++) {
  String s = wordarray[t];
  if (numberWord.contains(s) && isWordNumber(wordarray[t + 1]) {
      s = addCommas(wordarray[t + 1]);
  }
  buf.append(' ').append(s);
}
以歌曲疗慰 2024-12-08 01:12:04

此方法将解析数字并将它们放入逗号分隔的格式:

public string ParseNumbers(string expression)
{
    return string.Join(",", System.Text.RegularExpressions.Regex.Split(expression, "[^\\d]"));
}

This method will parse the numbers and put them in comma delimited format:

public string ParseNumbers(string expression)
{
    return string.Join(",", System.Text.RegularExpressions.Regex.Split(expression, "[^\\d]"));
}
一场春暖 2024-12-08 01:12:04

您需要向文本框添加一个事件(例如 KeyPress)处理程序。

TextBox1.KeyPress +=
                new KeyPressEventHandler(TextBox1_KeyPress);

void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
//do your check here.
}

You need to add an event(e.g KeyPress) handler to the text box.

TextBox1.KeyPress +=
                new KeyPressEventHandler(TextBox1_KeyPress);

void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
//do your check here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文