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

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/

10-15 10:07