You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#多字符串合并发送至WhatsApp失败问题求助

问题分析与解决方案

你的代码存在几个关键问题,导致无法将三个字符串作为整体发送到WhatsApp:

  • 循环条件counter < tempArray.Length-1直接漏掉了第三个字符串
  • 错误地把Keys.Enter枚举值拼接到字符串中,Keys.Enter是键盘按键的枚举标识,不是实际的换行符,WhatsApp完全无法识别
  • 逐个处理字符串再拼接的逻辑冗余,反而容易引入错误

正确的思路是先把三个字符串合并成一个包含合法换行的完整字符串,再统一发送。WhatsApp支持\r\n或Environment.NewLine作为同一消息内的换行分隔符,这样就能让三个字符串显示为一条消息里的多行内容。

修改后的代码示例

如果不需要额外的字符串清理逻辑,直接合并即可:

// 将所有行用换行符合并成一个完整字符串
string combinedMessage = string.Join(Environment.NewLine, richTextBox1.Lines);

// 直接使用combinedMessage发送到WhatsApp即可,比如模拟输入或调用API
System.Diagnostics.Debug.WriteLine(combinedMessage);

如果需要保留你的字符串清理逻辑,可调整为:

string[] tempArray = richTextBox1.Lines;
StringBuilder cleanedBuilder = new StringBuilder();

foreach (string line in tempArray)
{
    // 保留你的字符串清理逻辑
    char[] chary = new char[line.Length];
    line.CopyTo(0, chary, 0, line.Length);
    string cleanedLine = new string(chary);
    
    // 追加清理后的行并添加换行符
    cleanedBuilder.AppendLine(cleanedLine);
}

// 移除最后多余的换行符(可选)
string combinedMessage = cleanedBuilder.ToString().TrimEnd(Environment.NewLine.ToCharArray());

// 发送合并后的完整消息到WhatsApp

关键说明

  • 用string.Join或StringBuilder高效合并字符串,避免逐个拼接的性能问题
  • Environment.NewLine是跨平台兼容的换行符,WhatsApp能准确识别它来实现同一消息内的多行显示
  • 无需处理Keys枚举类,直接使用文本换行符就能满足需求

内容的提问来源于stack exchange,提问作者Abdooo Lek

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 09:12:45