C# - 从第一个空终止符开始修剪字符串 [英] C# - Trimming string from first null terminator and onwards

查看:24
本文介绍了C# - 从第一个空终止符开始修剪字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 C# 字符串 "RIP-1234-STOP\0\0\0\b\0\0\0???|B?Mp?\0\0\0" 从调用本地返回司机.

I have a C# string "RIP-1234-STOP\0\0\0\b\0\0\0???|B?Mp?\0\0\0" returned from a call to a native driver.

如何修剪从第一个空终止符 '\0\ 开始的所有字符.在这种情况下,我只想要RIP-1234-STOP".

How can I trim all characters from first null terminator '\0\ onwards. In this case, I just would like to have "RIP-1234-STOP".

谢谢.

推荐答案

这里有一个方法可以解决问题

Here is a method that should do the trick

string TrimFromZero(string input)
{
  int index= input.IndexOf('\0');
  if(index < 0)
    return input;

  return input.Substring(0,index);
}

这篇关于C# - 从第一个空终止符开始修剪字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆