简体   繁体   English

如何使用字符串开头的字符数分割字符串?

[英]How can I split a string using the number of characters from the beginning of the string?

How can I split a string using the number of characters from the beginning of the string? 如何使用字符串开头的字符数分割字符串?

Here is my code: 这是我的代码:

string website;

if (message.Contains("!help"))
{
    SendChatMessage("\n!resolve <url> - Resolve a website URL\n!spam <steamID> <message> - Spam a steam user\n!help - View commands list");
}
else if (message.Contains("!resolve"))
{
    website = message;
    SendChatMessage(website);
    SendChatMessage("Resolving...");
    SendChatMessage("I couldn't resolve that IP address! Sorry!");
}

It's messy and formatted badly granted, but I am new to C# and I was wondering if there is any way I can split the website string using the knowledge that the initial 8 characters will be: "!resolve" After this there will be a website url. 它是乱七八糟的,格式不正确,但是我是C#的新手,我想知道是否有任何办法可以使用以下8个字符来拆分网站字符串:“!resolve”在此之后将有一个网站网址。

Using Substring is the quickest and easiest way. 使用Substring是最快,最简单的方法。

var text = "!resolve http://www.google.com";
var url = text.Substring(8).Trim();

The first parameter of Substring is the zero-based index into the string you want to start copying from. Substring的第一个参数是要从其开始复制的字符串的从零开始的索引。 If you only specify that parameter, it will copy all of the text in the string starting from that index into a new string. 如果仅指定该参数,它将从该索引开始的字符串中的所有文本复制到新字符串中。

Split is based on characters or strings. 拆分基于字符或字符串。 If you want to split on position, you want substring: https://msdn.microsoft.com/en-us/library/system.string.substring%28v=vs.110%29.aspx 如果要拆分位置,则需要子字符串: https : //msdn.microsoft.com/zh-cn/library/system.string.substring%28v=vs.110%29.aspx

dotnetperls has a more user-friendly explanation: http://www.dotnetperls.com/substring dotnetperls的说明更加人性化: http : //www.dotnetperls.com/substring

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM