如果標題不清楚;我希望能夠從文本框中選擇任何字符,而無需製作一些複雜的循環相關代碼(我可以做到這一點)。例如,我們考慮將此文本輸入到文本框中:VB 2010:如何索引文本框(使它像插槽)?
hello user!
我想要一些語法,當我告訴給我弄指數1的值,它給我的「h」,對於指數5 =「O」 ...等
所以任何人都知道什麼是正確的語法,請幫忙!
如果標題不清楚;我希望能夠從文本框中選擇任何字符,而無需製作一些複雜的循環相關代碼(我可以做到這一點)。例如,我們考慮將此文本輸入到文本框中:VB 2010:如何索引文本框(使它像插槽)?
hello user!
我想要一些語法,當我告訴給我弄指數1的值,它給我的「h」,對於指數5 =「O」 ...等
所以任何人都知道什麼是正確的語法,請幫忙!
string
可以直接索引無需任何特殊代碼。
//from metadata
public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>
{
....
// Summary:
// Gets the character at a specified character position in the current System.String
// object.
//
// Parameters:
// index:
// A character position in the current string.
//
// Returns:
// A Unicode character.
//
// Exceptions:
// System.IndexOutOfRangeException:
// index is greater than or equal to the length of this object or less than
// zero.
public char this[int index] { get; }
....
}
dim str = "hello";
dim hchar = str(0);
dim echar = str(1);
dim lchar = str(2);
ect
Dim x As String = "Hello"
Console.Write(x.IndexOf("e")) 'Would return the position
Console.Write(x(x.IndexOf("e"))) 'Would return the character based upon the position
Console.Write(x(1)) 'Returns the character based at position 1 of the string
如果您使用WinForm,則可以刪除Console.Write。
TextBox1.Text = x(x.IndexOf("e"))
謝謝...祝你最好! – IWIH 2012-03-31 07:19:41
這應該起作用。
Dim orig = "hello user!"
Dim res = Enumerable.Range(0,orig.Length).[Select](Function(i) orig.Substring(i,1))
,那麼你可以這樣做:
Dim x = res(0) 'x = "h"
謝謝大家的幫助,em3ricasforsale給了我非常正確的答案......謝謝大家:-D ...我向大家致意。 – IWIH 2012-03-29 18:02:40
他的代碼是完全明智的,正如我下面所說的。字符串是直接索引的,只是說mystring(myindex),你得到的字符。使用它與mystring.ToArray()(myIndex)完全相同,它與mystring(myindex)完全相同! – asawyer 2012-03-29 18:10:04
「vb」標籤被逐漸淘汰,索引標籤還有其他更具體的與數據庫相關的含義。他們不屬於你的問題。 – 2012-04-01 02:52:44