簡體   English   中英

從字節數組轉換為字符串和拆分字符串

[英]Convert from byte array to string and Split String

假設我有一個字節數組

Byte[] arr;

然后,我將數組轉換為字符串1st。

String inputString = "";
foreach (Byte b in arr)
{
   if (b != 0)
      inputString += (Char) b;
   else inputString += " ";
}

假設字符串為:

inputString  = @"C:\Program Files\Test C:\Users\User A\AppData\Local\Temp C 32323 C:\Program Files\Test\Temp";

我希望將其分為以下四個字符串:

C:\Program Files\Test \\position 0 = test folder
C:\Users\User A\AppData\Local\Temp \\position 1 = windows temp folder
C 32323 \\position 2 = a name. It can be C2313 or C 2312 or whatever string
C:\Program Files\Test\Temp \\position 3 = temp for test folder
\\ position can change by me...

中間的每個字符串都將按空格分開。 這意味着我可以使用.Split('')。 但是,您知道某些路徑之間有空格,因此“ C:\\ Program Files \\ Test”就是一個示例。

如何獲得所需的值?

嘗試這樣做:

byte[] arr = ...

string[] inputStrings = Encoding.GetEncoding("iso-8859-1").GetString(arr).Split('\0');

並查看結果。

這是我的答案。 感謝@xanatos的建議。

String inputString = "";
bool isRepeat = false;
foreach (Byte b in arr)
{
   if (b != 0)
   {
      inputString += (Char)b;
      isRepeat = false;
   }
   else
   {
      if (!isRepeat)
      {
         inputString += "|";
         isRepeat = true;
      }
   }               
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM