Hi, I am looking for a string function (incase it exists) in VB.NET that will let me strip all but the first character from a string. for example if there is a string called "turtle", then after passing this string to the function, I should get a string that
has the single letter "t". Thanks,
Kulin Parikh
--
The more I learn about the Internet, the more amazed I am that it works at all.
The Left() function will do this. example: Dim aString as string = "This is a test string" Dim aFirstChar as string = Left(aString, 1) ' aFirstChar will contain "T" ' the second parameter is the number of characters, so ' Left(aString,4) will contain "This"
Steve Fabian
w: http://www.gooddogs.com/dotnetnuke
sit, stay, code ... good boy!
How about if i made use of something like this... from ur example: afirstchar As string = astring.Remove(2, (astring.Length - 1)) I've used the Remove function here. Thanks,
Kulin Parikh
--
The more I learn about the Internet, the more amazed I am that it works at all.
Here's the best ".NET way" of doing it... astring.SubString(0, 1) 0 being the first character and 1 being how many characters you want to grab FROM the first character The String data type in .NET is pretty powerful, you should check out everything it has in
it
True, but what I need is a function that will accept a variable as a parameter. The reason being I am taking the first name of a person from a textbox control called "txtFirstName". So I'm using the length property to get the length of the string (after trimming
it ofcourse), and having gotten this length, i want to split the string into the first character on the basis of the parameters I mentioned in the earlier post using the
Remove function. Right now, when i do that, it tells me that the count property of the
Remove method has to be an integer (inspite of my having passed a variable that is an integer - in this case Dim length As Integer). I guess it just wants a digit there no matter what. Any way around this? Thanks,
Kulin Parikh
--
The more I learn about the Internet, the more amazed I am that it works at all.
astring.Remove(2, astring.Length - 1) should work just fine...but it doesn't. So what I tried is :
'GENERATE THE UNIQUE USERNAME
'assign the text from the first name textbox to the string "letter"
Dim letter As String = txtFirstName.Text
Dim length As integer = letter.Length - 1
Dim name As String = ""
'removes all but the first letter from the string
While letter.Length > 1
name = letter.Remove(2, 1)
End While
Response.Write("Debug [length of string - 1]:" & name)
but it hangs right there. and times out with a "Server application not found" error. Whats wrong? Thanks,
Kulin Parikh
--
The more I learn about the Internet, the more amazed I am that it works at all.
Looks like this thread has been hanging for a while, so I'll jump in... Your code is hanging because you're stuck in an infinite loop. From the .NET SDK documentation on the System.String class: "A String is called immutable because its value cannot be modified
once it has been created. Methods that appear to modify a String actually return a new String containing the modification. If it is necessary to modify the actual contents of a string-like object, use the System.Text.StringBuilder class." So to make your code
work, you would need to replace that line in the loop with "letter = letter.Remove(2, 1)". However, you
definitely shouldn't need to write code like this...the problem you're seeing passing "astring.Length - 1" to the string.Remove() method is because strings in VB.NET (and .NET in general) are 0-based, not 1-based. Therefore, you need to call "astring.Remove(1,
astring.Length-1)"...otherwise, your count parameter extends beyond the end of the string. That said, I agree with HumanCompiler that a better way to do this is using the string.Substring() method. But either one will work. -- Ryan Milligan
BUT.... what if the length of the string is less than the required output length. eg strVest = "purple" The output I after is 'purp' so strVest .SubString(0, 4) is fine BUT what if strVest = "red" this ERR happens: *** Index and length must refer to a location
within the string. Parameter name: length *** Am I missing something here is it really so complex to do a LEFT() in .net Thanks a lot for any help guys
Left should work for you just fine I believe...or just put an if statement around it. I suppose you could make your own too...
Public Function Left(ByVal Value As String, ByVal Length As Integer) As String
If Value.Length >= Length Then
Return Value.SubString(0, Length)
Else
Return Value
End If
End Function
kulinp
Member
120 Points
24 Posts
String functions in VB.NET
Aug 26, 2002 07:30 PM|LINK
--
The more I learn about the Internet, the more amazed I am that it works at all.
steve.fabian
Participant
1414 Points
280 Posts
Re: String functions in VB.NET
Aug 26, 2002 07:43 PM|LINK
w: http://www.gooddogs.com/dotnetnuke
sit, stay, code ... good boy!
kulinp
Member
120 Points
24 Posts
Re: String functions in VB.NET
Aug 26, 2002 07:56 PM|LINK
--
The more I learn about the Internet, the more amazed I am that it works at all.
HumanCompile...
Member
511 Points
100 Posts
Microsoft
Re: String functions in VB.NET
Aug 26, 2002 09:24 PM|LINK
ASP.NET PM
http://about.me/erikporter
kulinp
Member
120 Points
24 Posts
Re: String functions in VB.NET
Aug 26, 2002 10:04 PM|LINK
--
The more I learn about the Internet, the more amazed I am that it works at all.
HumanCompile...
Member
511 Points
100 Posts
Microsoft
Re: String functions in VB.NET
Aug 26, 2002 10:10 PM|LINK
ASP.NET PM
http://about.me/erikporter
kulinp
Member
120 Points
24 Posts
Re: String functions in VB.NET
Aug 27, 2002 06:50 PM|LINK
'GENERATE THE UNIQUE USERNAME 'assign the text from the first name textbox to the string "letter" Dim letter As String = txtFirstName.Text Dim length As integer = letter.Length - 1 Dim name As String = "" 'removes all but the first letter from the string While letter.Length > 1 name = letter.Remove(2, 1) End While Response.Write("Debug [length of string - 1]:" & name)but it hangs right there. and times out with a "Server application not found" error. Whats wrong? Thanks,--
The more I learn about the Internet, the more amazed I am that it works at all.
Ryan Milliga...
Participant
1057 Points
210 Posts
Re: String functions in VB.NET
Aug 29, 2002 11:08 PM|LINK
FedUp
Member
15 Points
3 Posts
Re: String functions in VB.NET
Jan 27, 2004 11:06 AM|LINK
HumanCompile...
Member
511 Points
100 Posts
Microsoft
Re: String functions in VB.NET
Jan 27, 2004 03:10 PM|LINK
Public Function Left(ByVal Value As String, ByVal Length As Integer) As String If Value.Length >= Length Then Return Value.SubString(0, Length) Else Return Value End If End FunctionASP.NET PM
http://about.me/erikporter