c# - 如何在不复制逗号或任何其他标点符号的情况下将文本与单词分开?

标签 c# mysql

我正在使用下一个代码将文本与单词分开,然后将这些单词插入数据库。 问题是逗号也被复制了。 如何从逗号跳到复制的或任何其他标点符号?

var str = reader1.ReadToEnd();
string[] words = str.Split(' ');   //Insert all the song words into words named string
string constring1 = "datasource=localhost;port=3306;username=root;password=abc";

using (var conDataBase1 = new MySqlConnection(constring1))
{
    conDataBase1.Open();
    for (int i = 0; i < words.Length; i++)
      {
          int numberOfLetters = words[i].ToCharArray().Length; //Calculate the numbers of letters in each word  
          var songtext = "insert into myproject.words (word_text,word_length) values('" + words[i] + "','" + numberOfLetters + "');"; //Insert words list and length into words table
          MySqlCommand cmdDataBase1 = new MySqlCommand(songtext, conDataBase1);
          try
            { 
              cmdDataBase1.ExecuteNonQuery(); 
            }
            catch (Exception ex)
              {
                MessageBox.Show(ex.Message);  
              }
       }

 }

最佳答案

尝试用这个替换你的分割线:

 char [] separators = new char[] {' ', ',', '/'};
 var words = str.Split(separators, StringSplitOptions.RemoveEmptyEntries).ToArray();

您将获得不含标点符号的单词。您可以向 separators 数组中添加您想要删除的其他标点符号。我输入了空格、逗号和 /

关于c# - 如何在不复制逗号或任何其他标点符号的情况下将文本与单词分开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34838822/

相关文章:

c# - 比较使用 Thread.Sleep 和 Timer 延迟执行

mysql - 在 SQL 中添加列和修剪数据

mysql - 选择和更新记录时查询为空错误?

mysql - 如何在 SQL 查询的 WHERE 子句中使用表值?

c# - 使用 C# 调用带参数的 SQL Server 存储过程

c# - Func<> 为结果赋值

c# - 如何在 asp.net 的 ViewState 中存储数组?

c# - mono mcs 'Winforms Hello World' 给出编译错误 CS006 : Metadata file 'cscompmgd.dll' could not be found

javascript - 在 mysql 中使用唯一键 - Sequelize

php - 如何从我的数据库中的表中选择 [somename]