用字串分割的時候只能分割成前四段,再多了就會提醒陣列超過索引,是因為我用utf32編碼的原因嗎

uj5u.com熱心網友回復:
把你的getstring內容發出來uj5u.com熱心網友回復:
不會循環字串數組的嗎?你那個數組是寫死的,當然會出現你這個問題uj5u.com熱心網友回復:
Getstring是串口發給緩沖區的資料uj5u.com熱心網友回復:
就算是串口讀的,也要學會寫單元測驗,或者簡單寫個測驗程式,看看你這個函式能正常作業不能,構造一個輸入,看看輸出是不是符合預期uj5u.com熱心網友回復:
與分割沒有關系 。比如字串: 1,2,3,4
分割后,只有4個陣列成員,你要訪問5個成員,當然出錯了。
所以這是因為你的字串的原因。使用前,先檢查資料格式,再使用。
uj5u.com熱心網友回復:
//
// 摘要:
// 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);
uj5u.com熱心網友回復:
取陣列賦值先判斷一下陣列的長度后再賦值。看你的寫法回傳格式一定能分出四組嗎。如果不是的話,賦值那邊就不應該寫死下標uj5u.com熱心網友回復:
額,老問題。所以還是老回答,和splite無關。和串口有關,串口從來就不是你認為的人家發“abc”你就應該收一個"abc"的,也許你get一次只收到一個"a",然后再get一次又收到一個"bc"
所以你想當然認為一定就是4個,顯然不對。
正確的做法,根據協議分割,符合協議的做你要做的事情,暫時不符合的快取到下一次判定(當然對于你目前我們只能這么說,在搞啥記憶體復用,狀態機,NIO一類的你更糊涂,還不如直接說給個大快取,符合移除并通知處理,不符合的留在那里等待下次一起判定)
uj5u.com熱心網友回復:
根據商量好的格式進行分割,傳過來的時候一定要有"";"";"";"",如果是隨意長度的就要判斷陣列的個數,不然傳三個就會出現這種情況轉載請註明出處,本文鏈接:https://www.uj5u.com/net/131086.html
標籤:C#
上一篇:為什么插入斷點不進入if陳述句
下一篇:答疑帖
