C# 用 ip 地址和端口修剪字符串
可能的重复:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就我个人而言,我会使用
Substring
:Mark 提到使用
string.Split
这也是一个不错的选择 - 但你应该检查零件的数量:或者如果你是对包含冒号的端口部分感到满意(如我的
Substring
版本所示),那么您可以使用:另一种替代方法是使用正则表达式将两个部分作为组进行匹配。老实说,我想说这在目前来说有点过分了,但如果事情变得更加复杂,它可能会成为一个更有吸引力的选择。 (我倾向于使用简单的字符串操作,直到事情开始变得棘手并且变得更加“模式化”,此时我带着一些恐惧打破了正则表达式。)
Personally I'd use
Substring
:Mark mentioned using
string.Split
which is a good option too - but then you should probably check the number of parts:Or if you're happy with the port part containing a colon (as my
Substring
version does) then you can use: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.)
我会使用 string.Split() :
I would use
string.Split()
:尝试
String.Split
:这将把
parts[0]
中的 IP 地址和parts[1]
中的端口。Try
String.Split
:This will put the IP address in
parts[0]
and the port inparts[1]
.