How to find the space using substring to display initials

15,799

Solution 1

Q: How do I find a space (" ") in a string with VB.Net?

A: String.IndexOf (" ");

Q: How do I find subsequent spaces in the string?

A: Extract the substring of everything to the right of the first space you found.

Then do an ".IndexOf (" ")" of that substring.

Until you get to the end of the string, or until you get bored :)

Solution 2

I apologize for not writing it in VB but I am more familiar with C#. There is no reason to look for the index of a space. Just split the string on the space and then pull the first letter of each of the results in the array. For simplicity, I made an assumption that it was only two words in the string but you could loop through the array of results to put out all the initials if they included a middle name:

string name = "Test User";
string[] nameParts = name.Split(' ');
string initials = nameParts[0].Substring(0, 1) + nameParts[1].Substring(0, 1);

VB Version:

 Dim name As String = "Test User"
 Dim nameParts As String() = name.Split(" ")
 Dim initials As String = nameParts(0).Substring(0, 1) & nameParts(1).Substring(0, 1)

Again this is a very simplistic approach to what you are asking. As @paxdiablo pointed out in his comments there are variations of names that will not match this pattern but you know you're program.

Share:
15,799
Tycholiz
Author by

Tycholiz

I went to school to study finance because I loved the idea of working in the business world with an emphasis on the thrill of numbers. My first few months on the job had nothing to do with the prior conception of the field. I knew that while I loved business, I despised the corporate slow-paced style of work, so I quickly jumped ship and taught myself to code, eventually taking a 10-week boot camp. Since that time, I have learned technologies from a vastly different areas of the programming universe,including web apps, bash scripts, web scrapers, Android apps... I am very much a generalist, because my value lies in being able to see the picture, whether programming or not. This allows me to see opportunities that may otherwise be missed due to focusing too deeply on a small subset of components. Truly, my biggest passion is spotting kinks in a grand system, and finding ways to make those kinks among the most robust components of the entire system.

Updated on June 04, 2022

Comments

  • Tycholiz
    Tycholiz about 2 years

    The part of the program I'm trying to write is supposed to take the users input, which is their name, and in a listbox spit out their initials. I have so far:

    Dim initials As String
    Dim indexspace As Integer
    initials = name.Substring(0, 1)
    indexspace = name.IndexOf(" ")
    lstInvoice.Items.Add(String.Format(strFormat, "Invoice ID", initials & space))
    

    When I run the program, I can get the first initial to pop up, but I am not certain how to get the second to pop up.

    Thank you very much for your assistance on this matter, it is much appreciated