C# 用 ip 地址和端口修剪字符串

发布于 2024-12-07 19:35:20 字数 261 浏览 0 评论 0原文

可能的重复:
c# 中的字符串分割

大家好,我正在从套接字获取连接的 IP 地址,如下所示: >>> “188.169.28.103:61635”如何将IP地址放入一个字符串中并将端口放入另一个字符串中? 谢谢。

Possible Duplicate:
string split in c#

Hello guys i am getting connected ip address from socket which is looks like this: >> "188.169.28.103:61635" how i can put ip address into one string and port into another?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温折酒 2024-12-14 19:35:20

就我个人而言,我会使用 Substring

int colonIndex = text.IndexOf(':');
if (colonIndex == -1)
{
    // Or whatever
    throw new ArgumentException("Invalid host:port format");
}
string host = text.Substring(0, colonIndex);
string port = text.Substring(colonIndex + 1);

Mark 提到使用 string.Split 这也是一个不错的选择 - 但你应该检查零件的数量:

string[] parts = s.Split(':');
if (parts.Length != 2)
{
    // Could be just one part, or more than 2...
    // throw an exception or whatever
}
string host = parts[0];
string port = parts[1];

或者如果你是对包含冒号的端口部分感到满意​​(如我的 Substring 版本所示),那么您可以使用:

// Split into at most two parts
string[] parts = s.Split(new char[] {':'}, 2);
if (parts.Length != 2)
{
    // This time it means there's no colon at all
    // throw an exception or whatever
}
string host = parts[0];
string port = parts[1];

另一种替代方法是使用正则表达式将两个部分作为组进行匹配。老实说,我想说这在目前来说有点过分了,但如果事情变得更加复杂,它可能会成为一个更有吸引力的选择。 (我倾向于使用简单的字符串操作,直到事情开始变得棘手并且变得更加“模式化”,此时我带着一些恐惧打破了正则表达式。)

Personally I'd use Substring:

int colonIndex = text.IndexOf(':');
if (colonIndex == -1)
{
    // Or whatever
    throw new ArgumentException("Invalid host:port format");
}
string host = text.Substring(0, colonIndex);
string port = text.Substring(colonIndex + 1);

Mark mentioned using string.Split which is a good option too - but then you should probably check the number of parts:

string[] parts = s.Split(':');
if (parts.Length != 2)
{
    // Could be just one part, or more than 2...
    // throw an exception or whatever
}
string host = parts[0];
string port = parts[1];

Or if you're happy with the port part containing a colon (as my Substring version does) then you can use:

// Split into at most two parts
string[] parts = s.Split(new char[] {':'}, 2);
if (parts.Length != 2)
{
    // This time it means there's no colon at all
    // throw an exception or whatever
}
string host = parts[0];
string port = parts[1];

Another alternative would be to use a regular expression to match the two parts as groups. To be honest I'd say that's overkill at the moment, but if things became more complicated it may become a more attractive option. (I tend to use simple string operations until things start getting hairy and more "pattern-like", at which point I break out regular expressions with some trepidation.)

疯狂的代价 2024-12-14 19:35:20

我会使用 string.Split() :

var parts = ip.Split(':');
string ipAddress = parts[0];
string port = parts[1];

I would use string.Split():

var parts = ip.Split(':');
string ipAddress = parts[0];
string port = parts[1];
日裸衫吸 2024-12-14 19:35:20

尝试String.Split

string[] parts = s.Split(':');

这将把parts[0] 中的 IP 地址和 parts[1] 中的端口。

Try String.Split:

string[] parts = s.Split(':');

This will put the IP address in parts[0] and the port in parts[1].

久夏青 2024-12-14 19:35:20
string test = "188.169.28.103:61635";

string [] result  = test.Split(new char[]{':'});

string ip = result[0];
string port = result[1];
string test = "188.169.28.103:61635";

string [] result  = test.Split(new char[]{':'});

string ip = result[0];
string port = result[1];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文