Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys . how can i get the first,last and middle part of the string in ASP VB ?

for example:

Lebron King James.

i want to get the "Lebron","King","James" separately .

What I have tried:

I tried to use left trim. but i think it is difficult to use in getting the middle string.
Posted
Updated 8-Jan-17 1:40am
Comments
[no name] 8-Jan-17 7:07am    
Look at (and read) the documentation for the String class and see if there is a method you could use to do this. I would suggest the Split method myself....
Tomas Takac 8-Jan-17 7:09am    

You can use String.Split Method (System)[^]

Consider the following example
VB
Dim input As String
Dim elements As String()

input = "Lebron King James"

elements = input.Split(" ")
For Each item As String In elements
    Console.WriteLine(item)
Next

Output would be
Lebron
King
James
 
Share this answer
 
Quote:
I tried to use left trim. but i think it is difficult to use in getting the middle string.

If you look at the documentation (string functions), you will see that left is not the only function to cut strings.
 
Share this answer
 
How do you define "first,last and middle part of the string" in
i want to get the "Lebron","King","James" separately .

Look for common traits shared by the result that you want and I noticed they are double-quoted individually, then call Regex to extract them. See example:
Imports System
Imports System.Text.RegularExpressions

Public Module Module1

    Public Sub Main()
        Dim str As String = "i want to get the ""Lebron"",""King"",""James"" separately."
		Console.Write("The sentence is: ")
		Console.WriteLine(str)
		Dim pattern As String = "(?<="")[^,""]+(?="")"
		Console.Write("The Regex pattern is: ")
        Console.WriteLine(pattern)

		showMatch(str, pattern)
		
    End Sub

    Sub showMatch(ByVal text As String, ByVal expr As String)
        Dim mc As MatchCollection = Regex.Matches(text, expr)
        Dim m As Match
		
		Console.WriteLine("The matched are as follows: ")
        For Each m In mc
            Console.WriteLine(m)
        Next m
    End Sub
End Module
The result as shown:
The sentence is: i want to get the "Lebron","King","James" separately.
The Regex pattern is: (?<=")[^,"]+(?=")
The matched are as follows: 
Lebron
King
James
Learn The 30 Minute Regex Tutorial[^]
 
Share this answer
 
Comments
[no name] 8-Jan-17 7:52am    
Might work if that was actually the situation the OP was asking about.
Peter Leow 8-Jan-17 7:58am    
If not, let it be knowledge sharing and learning.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900