Lỗi mẫu học bổng xin vui lòng giải quyết vấn đề của tôi

lập trình


System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length at System.String.Substring(Int32 startIndex, Int32 length) at ScholarFormDetail1112122.btn_submit_Click(Object sender, EventArgs e)

Những gì tôi đã thử:

System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length at System.String.Substring(Int32 startIndex, Int32 length) at ScholarFormDetail1112122.btn_submit_Click(Object sender, EventArgs e)

Giải pháp 1

Hãy xem các ví dụ sau:

public class Program
{
	public static void Main()
	{
		string test= "0123456789";
		
		// 1.) Will fail, because max. Index will be 9
		// string subString0= test.Substring(10, 1);
		
		// 2.) Ok
		string subString1= test.Substring(9, 1);
		
		// 3.) Will fail because  there is only one character available. See note *1)
		// string subString2= test.Substring(9, 100);

		// 4.) Ok
		string subString3= test.Substring(9, 1);
	}
}

Lưu ý *1)
Có vấn đề để ném vào trường hợp đó một ngoại lệ. Hầu hết các ngôn ngữ khác sẽ chỉ trả về các ký tự có sẵn.

[Edit]

Lưu ý *1), Cảm ơn Richard vì gợi ý của anh ấy.
Một số ngôn ngữ không đưa ra Ngoại lệ trong trường hợp đó.

Tôi hy vọng nó có ích.

コメント

タイトルとURLをコピーしました