Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.Net String remove everthing after second space in string

Posted on 2010-08-23
18
Medium Priority
?
1,087 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Technology Partners: 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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

610 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