[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.Net String remove everthing after second space in string

Posted on 2010-08-23
18
Medium Priority
?
1,108 Views
Last Modified: 2013-11-27
Hello I am trying to query from VB.Net, but were I pull my data it gives me full name including lastname, firstname and middle name.

I would like to remove everthing after 2nd white space example
String Sampson, Robert Steve should just be Sampson, Robert
and
String Sahdala, Lesbia P should just be Sahdala, Lesbia

It seems like something I should be able to do with trim command. It seems like a simple thing but can't seem to figure out Syntax.

Anyone have any ideas or maybe maybe have another way
0
Comment
Question by:marcuschang
  • 6
  • 5
  • 4
  • +2
18 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33505815
Dim s as String = "String Sampson, Robert Steve should just be Sampson, Robert"
Dim a() = s.Split(" ")
If a.Length > 2 Then
  s = a(1) + a(2)
End If  ' else s is unchanged
0
 

Author Comment

by:marcuschang
ID: 33506013
Getting closer, but running the above code gives me RobertSteve
would need it to be Sampson, Robert
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33506028
Try this

        Dim s As String = "Sampson, Robert Steve "
        s = If(s.Split(" ").Length > 2, s.Split(" ")(1) + s.Split(" ")(2), Nothing)
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 17

Expert Comment

by:nepaluz
ID: 33506038
Or just add a comma and get

        Dim s As String = "Sampson, Robert Steve "
        s = If(s.Split(" ").Length > 2, s.Split(" ")(1) + "," + s.Split(" ")(2), Nothing)
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 1000 total points
ID: 33506039
I can't index :(

Dim s as String = "String Sampson, Robert Steve should just be Sampson, Robert"
Dim a() = s.Split(" ")
If a.Length > 2 Then
  s = a(0) + a(1)
End If  ' else s is unchanged
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33506063
>>         s = If(s.Split(" ").Length > 2, s.Split(" ")(1) + s.Split(" ")(2), Nothing)

Yes you can write it in one line, but I prefer not to use split more times than required, especially if the string can be quite long; each split create a new array.
0
 

Author Comment

by:marcuschang
ID: 33506209
It seems like it should work but it keeps giving back middle name as first
Error.JPG
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 33506283
You could do something like this to:

        Dim supvpovmove As String = "Sampson, Robert Steve"
        Dim values As New List(Of String)
        values.AddRange(supvpovmove.Split(" "))
        While values.Count > 2
            values.RemoveAt(2)
        End While
        supvpovmove = String.Join(" ", values.ToArray)
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 33506285
IF you continue debugging you will get the right value
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 33506348
On the image that you show above on S you get the right value but till you step over and go to the next step you wont get on your variable the right value that is S
0
 

Author Comment

by:marcuschang
ID: 33506367
This did the trick it converted Sampson, Robert Steve to Sampson, Robert. Awesome thanks for the direction everyone. I think cyberkiwi: had 99% of the answer just needed to start array at 0    
 'start convert trim middle
        Dim s As String = supvpovmove
        Dim a() = s.Split(" ")
        If a.Length > 2 Then
            s = a(0) + " " + a(1)
        End If
        supvpovmove = s
        'end convert trim middle
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33506570
Like I corrected in http:#a33506039 above, you mean?
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33506667
Give the man hos points! He really needs them.
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33506690
nepaluz,

Just to let you know, I am deeply offended by your comment.

You posted two solutions 25 minutes after the first comment that somehow inherited the same bug (index start at 1) but introduced inefficiencies, then added an unnecessary comment http:#a33506667.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33506732
I assumed (naturally) that you had posted tested code; I in turn offered a one-line solution which has clearly rubbed you the wrong way (and apologise for that I will NOT).

To stick with the question, would this rile you even more?

        Dim s As String = "Sampson, Robert Steve "
        Dim result = s.Split(" ")
        s = If(result.Length > 2, result(0) + "," + result(1), Nothing)
Dim s As String = "Sampson, Robert Steve "
        Dim result = s.Split(" ")
        s = If(result.Length > 2, result(0) + "," + result(1), Nothing)

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33506826
@marcus

Using Split is very easy to understand.  I apologize, http:#a33506039 did not put a space between the 2 words.  Below is a function that will perform faster if your source string is very long - it does not need to build an array and only scans as many letters as required.

supvpovmove is unchanged if there are less than 2 spaces.

@nepaluz

Can't we just get along?
I was commenting on your solution, take it for what it is.
The intention is to return whatever is left of the 2nd space, not turn it into Nothing if there is only 1 space or none.


        Dim supvpovmove As String = "Sampson, Robert Steve should just be Sampson, Robert"
        Dim firstSpace, secondSpace
        firstSpace = supvpovmove.IndexOf(" ")
        If firstSpace > 0 Then
            secondSpace = supvpovmove.IndexOf(" ", firstSpace + 1)
            If secondSpace > 0 Then supvpovmove = supvpovmove.Substring(0, secondSpace - 1)
        End If
        'MsgBox(supvpovmove)

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33506895
I believe a solution has been posted for this question so I will stop monitoring pronto. (and save myself the handbags too)
0
 

Author Comment

by:marcuschang
ID: 33507344
Yes cyberkiwi you did correct. This solution fits perfectly because the names should never be more then middle name or initial  will refer back to this page though if I run into something funky, where multiple middle names show up, but don' t think it will good information here.  Thanks again
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month20 days, 5 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question