Solved

VB.Net String remove everthing after second space in string

Posted on 2010-08-23
18
1,018 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
 
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 250 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 85

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now