2012-03-29 55 views
1

如果標題不清楚;我希望能夠從文本框中選擇任何字符,而無需製作一些複雜的循環相關代碼(我可以做到這一點)。例如,我們考慮將此文本輸入到文本框中:VB 2010:如何索引文本框(使它像插槽)?

hello user!

我想要一些語法,當我告訴給我弄指數1的值,它給我的「h」,對於指數5 =「O」 ...等

所以任何人都知道什麼是正確的語法,請幫忙!

+0

謝謝大家的幫助,em3ricasforsale給了我非常正確的答案......謝謝大家:-D ...我向大家致意。 – IWIH 2012-03-29 18:02:40

+0

他的代碼是完全明智的,正如我下面所說的。字符串是直接索引的,只是說mystring(myindex),你得到的字符。使用它與mystring.ToArray()(myIndex)完全相同,它與mystring(myindex)完全相同! – asawyer 2012-03-29 18:10:04

+0

「vb」標籤被逐漸淘汰,索引標籤還有其他更具體的與數據庫相關的含義。他們不屬於你的問題。 – 2012-04-01 02:52:44

回答

3

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 
+0

哦!最好的......非常簡單,對於我所需要的工作:-D – IWIH 2012-03-29 18:08:57

+0

謝謝asawyer,祝你在編程生活中獲得最佳效果。 – IWIH 2012-03-29 18:10:12

0
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")) 
+0

謝謝...祝你最好! – IWIH 2012-03-31 07:19:41

0

這應該起作用。

Dim orig = "hello user!" 
Dim res = Enumerable.Range(0,orig.Length).[Select](Function(i) orig.Substring(i,1)) 

,那麼你可以這樣做:

Dim x = res(0) 'x = "h" 
+0

我不得不說,我覺得這個解決方案很奇怪。 – asawyer 2012-03-29 17:51:34

+0

謝謝em3ricasforsale, 它做得很好......但是,我不明白這是什麼意思! :-P ...沒關係,我會仔細研究它,並在真主的意志中,我會理解它...... 謝謝你從我心底深處...... – IWIH 2012-03-29 17:58:34