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
Solved

VB.Net String remove everthing after second space in string

Posted on 2010-08-23
18
1,033 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

829 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