Solved

VB.Net String remove everthing after second space in string

Posted on 2010-08-23
18
1,055 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

705 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