第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

請大佬幫看一下C#里的SubString();,謝謝!

請大佬幫看一下C#里的SubString();,謝謝!

智慧大石 2021-10-12 14:11:24
static void Main(string[] args){string str1 = Console.ReadLine();string str2 = "";byte[] byt=new byte[100];byt = Encoding.Unicode.GetBytes(str1);str2 = Encoding.Unicode.GetString(byt).Substring (0,byt.Length );Console.Write(str2);Console.ReadLine();}這里出現(xiàn)未處理 System.ArgumentOutOfRangeExceptionMessage="索引和長度必須引用該字符串內(nèi)的位置。\r\n參數(shù)名: length"Source="mscorlib"ParamName="length"StackTrace:在 System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)在 System.String.Substring(Int32 startIndex, Int32 length)在 ConsoleApplication15.Program.Main(String[] args) 位置 E:\chen\Visual Studio 2008\Projects\文件\ConsoleApplication15\ConsoleApplication15\Program.cs:行號 17在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)在 System.Threading.ThreadHelper.ThreadStart()InnerException:請問是怎么回事?
查看完整描述

2 回答

?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

string str1 = "dsfadfasdfasdfasdfasdfasdfasdfasdf";
string str2 = "";
byte[] byt = new byte[100];
byt = Encoding.Unicode.GetBytes(str1);

str2 = Encoding.Unicode.GetString(byt).Substring(0, byt.Length);
Console.Write(str2);
Console.ReadLine();
上邊是我假設(shè)的,此時(shí)假設(shè) str1為 "dsfadfasdfasdfasdfasdfasdfasdfasdf"
雖然你定義了 byte[] byt = new byte[100];
但是GetBytes在生成字節(jié)序列的時(shí)候,填充了數(shù)組的前68個(gè),后邊的都是空的,
這樣解碼為str2的時(shí)候他的dsfadfasdfasdfasdfasdfasdfasdfasdf的長度還是原來的長度,但是你的byt.Length是100,這樣就造成了越界了,肯定會報(bào)錯的 ,
建議你數(shù)組可以使用arraylist可變長數(shù)組,

查看完整回答
反對 回復(fù) 2021-10-16
?
楊魅力

TA貢獻(xiàn)1811條經(jīng)驗(yàn) 獲得超6個(gè)贊

byt的長度是str1轉(zhuǎn)換字節(jié)后的長度,并不是str1的長度,所以可能會引起越界
起始位置和長度要根據(jù)str1.length來斟酌

查看完整回答
反對 回復(fù) 2021-10-16
  • 2 回答
  • 0 關(guān)注
  • 350 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號