我有一些为人工智能编写的c#代码
else if (msg.Body.Contains("ai"))
{
string ai = msg.Body.Replace("!ai ", string.Empty).Replace(" ", "+");
WebClient _client = new WebClient();
msg.Chat.SendMessage("Getting AI... ");
_client.Headers["User-Agent"] = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
//Define what the client is heading to.
string a = _client.DownloadString("!Api here!" + ai).Replace("string", "\n");
//Display Message
msg.Chat.SendMessage(Nick + "AI says: " + a);
}但是api返回了2行,我只需要第一行,因为这是我使用的唯一行,请帮助!
发布于 2015-06-05 16:25:33
您还可以使用字符串数据类型提供的简单.Remove()函数,如下所示:
int index = a.IndexOf("string");
a = a.Remove(index, a.Length - index);如果希望在新行之后删除所有内容,则可以使用"string"替换IndexOf方法中的Environment.NewLine,因为IndexOf将查找指定参数的第一个实例。
发布于 2015-06-05 16:23:39
尝试:
a = a.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None).First();https://stackoverflow.com/questions/30671225
复制相似问题