C#split字符串切割

weixin_45380906 2019-07-16 12:09:10
用字符串分割的时候只能分割成前四段,再多了就会提醒数组超过索引,是因为我用utf32编码的原因吗
...全文
327 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
牧歌ing 2019-07-23
  • 打赏
  • 举报
回复
根据商量好的格式进行分割,传过来的时候一定要有"";"";"";"",如果是随意长度的就要判断数组的个数,不然传三个就会出现这种情况
  • 打赏
  • 举报
回复
把你的getstring内容发出来
wanghui0380 2019-07-16
  • 打赏
  • 举报
回复
额,老问题。 所以还是老回答,和splite无关。和串口有关,串口从来就不是你认为的人家发“abc”你就应该收一个"abc"的,也许你get一次只收到一个"a",然后再get一次又收到一个"bc" 所以你想当然认为一定就是4个,显然不对。 正确的做法,根据协议分割,符合协议的做你要做的事情,暂时不符合的缓存到下一次判定(当然对于你目前我们只能这么说,在搞啥内存复用,状态机,NIO一类的你更糊涂,还不如直接说给个大缓存,符合移除并通知处理,不符合的留在那里等待下次一起判定)
情似生活 2019-07-16
  • 打赏
  • 举报
回复
取数组赋值先判断一下数组的长度后再赋值。看你的写法返回格式一定能分出四组吗。如果不是的话,赋值那边就不应该写死下标
XBodhi. 2019-07-16
  • 打赏
  • 举报
回复
  //
// 摘要:
// Splits a string into a maximum number of substrings based on the characters in
// an array.
//
// 参数:
// separator:
// A character array that delimits the substrings in this string, an empty array
// that contains no delimiters, or null.
//
// count:
// The maximum number of substrings to return.
//
// options:
// System.StringSplitOptions.RemoveEmptyEntries to omit empty array elements from
// the array returned; or System.StringSplitOptions.None to include empty array
// elements in the array returned.
//
// 返回结果:
// An array whose elements contain the substrings in this string that are delimited
// by one or more characters in separator. For more information, see the Remarks
// section.
//
// 异常:
// T:System.ArgumentOutOfRangeException:
// count is negative.
//
// T:System.ArgumentException:
// options is not one of the System.StringSplitOptions values.
public String[] Split(char[] separator, int count, StringSplitOptions options);
//
// 参数:
// separator:
//
// count:
//
// options:
public String[] Split(char separator, int count, StringSplitOptions options = StringSplitOptions.None);
//
// 参数:
// separator:
//
// options:
public String[] Split(char separator, StringSplitOptions options = StringSplitOptions.None);
//
// 摘要:
// Splits a string into substrings that are based on the characters in an array.
//
// 参数:
// separator:
// A character array that delimits the substrings in this string, an empty array
// that contains no delimiters, or null.
//
// 返回结果:
// An array whose elements contain the substrings from this instance that are delimited
// by one or more characters in separator. For more information, see the Remarks
// section.
public String[] Split(params char[] separator);
//
// 摘要:
// Splits a string into a maximum number of substrings based on the characters in
// an array. You also specify the maximum number of substrings to return.
//
// 参数:
// separator:
// A character array that delimits the substrings in this string, an empty array
// that contains no delimiters, or null.
//
// count:
// The maximum number of substrings to return.
//
// 返回结果:
// An array whose elements contain the substrings in this instance that are delimited
// by one or more characters in separator. For more information, see the Remarks
// section.
//
// 异常:
// T:System.ArgumentOutOfRangeException:
// count is negative.
public String[] Split(char[] separator, int count);
//
// 摘要:
// Splits a string into substrings based on the characters in an array. You can
// specify whether the substrings include empty array elements.
//
// 参数:
// separator:
// A character array that delimits the substrings in this string, an empty array
// that contains no delimiters, or null.
//
// options:
// System.StringSplitOptions.RemoveEmptyEntries to omit empty array elements from
// the array returned; or System.StringSplitOptions.None to include empty array
// elements in the array returned.
//
// 返回结果:
// An array whose elements contain the substrings in this string that are delimited
// by one or more characters in separator. For more information, see the Remarks
// section.
//
// 异常:
// T:System.ArgumentException:
// options is not one of the System.StringSplitOptions values.
public String[] Split(char[] separator, StringSplitOptions options);
//
// 参数:
// separator:
//
// count:
//
// options:
public String[] Split(String separator, int count, StringSplitOptions options = StringSplitOptions.None);
//
// 参数:
// separator:
//
// options:
public String[] Split(String separator, StringSplitOptions options = StringSplitOptions.None);
//
// 摘要:
// Splits a string into a maximum number of substrings based on the strings in an
// array. You can specify whether the substrings include empty array elements.
//
// 参数:
// separator:
// A string array that delimits the substrings in this string, an empty array that
// contains no delimiters, or null.
//
// count:
// The maximum number of substrings to return.
//
// options:
// System.StringSplitOptions.RemoveEmptyEntries to omit empty array elements from
// the array returned; or System.StringSplitOptions.None to include empty array
// elements in the array returned.
//
// 返回结果:
// An array whose elements contain the substrings in this string that are delimited
// by one or more strings in separator. For more information, see the Remarks section.
//
// 异常:
// T:System.ArgumentOutOfRangeException:
// count is negative.
//
// T:System.ArgumentException:
// options is not one of the System.StringSplitOptions values.
public String[] Split(String[] separator, int count, StringSplitOptions options);
//
// 摘要:
// Splits a string into substrings based on the strings in an array. You can specify
// whether the substrings include empty array elements.
//
// 参数:
// separator:
// A string array that delimits the substrings in this string, an empty array that
// contains no delimiters, or null.
//
// options:
// System.StringSplitOptions.RemoveEmptyEntries to omit empty array elements from
// the array returned; or System.StringSplitOptions.None to include empty array
// elements in the array returned.
//
// 返回结果:
// An array whose elements contain the substrings in this string that are delimited
// by one or more strings in separator. For more information, see the Remarks section.
//
// 异常:
// T:System.ArgumentException:
// options is not one of the System.StringSplitOptions values.
public String[] Split(String[] separator, StringSplitOptions options);
xiaoid 2019-07-16
  • 打赏
  • 举报
回复
与分割没有关系 。
比如字符串: 1,2,3,4
分割后,只有4个数组成员,你要访问5个成员,当然出错了。
所以这是因为你的字符串的原因。使用前,先检查数据格式,再使用。
palhotel 2019-07-16
  • 打赏
  • 举报
回复
引用 3 楼 weixin_45380906 的回复:
Getstring是串口发给缓冲区的数据
就算是串口读的,也要学会写单元测试,或者简单写个测试程序,看看你这个函数能正常工作不能,构造一个输入,看看输出是不是符合预期
weixin_45380906 2019-07-16
  • 打赏
  • 举报
回复
Getstring是串口发给缓冲区的数据
ManBOyyy 2019-07-16
  • 打赏
  • 举报
回复
不會循環字符串數組的嗎?你那個數組是寫死的,當然會出現你這個問題

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧