Extract all first characters of any word in a text

Posted on 2014-01-24
Last Modified: 2014-01-24
I have to write a programm that given a text (in a textbox), when runing, the result in another textbox is the first letter of every word the original text has in it. Ideally, the result shoud be in UPPER case and should have all points the original text have.

Textbox1.Text= "Our Father in heaven, hallowed be your name.
Your kingdom come, your will be done, on earth, as it is in heaven.
Give us this day our daily bread, and forgive us our debts, as we also have forgiven our debtors.
And lead us not into temptation, but deliver us from evil."

Result in Textbox2.Text should be the following:


I was thinking to use the Substring(0,1) function.
Question by:José Perez
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
LVL 52

Expert Comment

by:Carl Tawn
ID: 39806486
That would work. Simply split the string on the space character, to get an array, loop through and use Substring(0,1) to grab the first character from each word.

You could also do it with a Linq query, but that would depend on which version of .Net you are using.
LVL 51

Expert Comment

ID: 39806499

You could first clean the string to have only points as punctuation and then use split

and create a str looping your array with Substring(0,1)

LVL 26

Expert Comment

by:Shaun Kline
ID: 39806507
The results in your example is also including sentence punctuation. Is that also a requirement of your program?

The simplest means to do what you are looking for is to use the string.Split function and loop through the resulting array. If you need to include punctuation, you would split on the sentence terminator (period, question mark, exclamation mark) and then split on the space for each sentence in the array.
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Author Comment

by:José Perez
ID: 39806668
Carl Tawn:
Think we're almost done... I have coded the following, and put the result in a Messagebox and it is displaying each word as I desired, but now I have to write it to the Textbox2... can you tell how to put all together in just 1 string?

Public Class LetraXpalabra

    Private Sub btnAnalizar_Click(sender As Object, e As EventArgs) Handles btnAnalizar.Click
        Dim texto As String = txtTextoOriginal.Text 'original text
        Dim b As String() = texto.Split(" ".ToCharArray)
        'Dim a As String = a

        For Each palabra In b
            MessageBox.Show(palabra.Substring(0, 1))
    End Sub
End Class

Open in new window

LVL 52

Expert Comment

by:Carl Tawn
ID: 39806685
Change your For to:
Dim sb As New StringBuilder()
For Each palabra In b
    sb.Append(palabra.Substring(0, 1))

txtOutput.Text = sb.ToString()

Open in new window


Author Comment

by:José Perez
ID: 39806695 does not recognize "StringBuilder" should I have to import some library?
LVL 52

Accepted Solution

Carl Tawn earned 200 total points
ID: 39806711
It's in the System.Text namespace, so you might need to either add an Import directive at the top of your page:
Imports System.Text

Open in new window

Or, use the fully qualified name:
Dim sb As New System.Text.StringBuilder()

Open in new window

LVL 40
ID: 39807831
Good luck in deciphering the old lady prayer cards (that is what you are doing, isn't it?).

I read about that and it is both a fascinating and moving story.

Author Comment

by:José Perez
ID: 39807893
With this code it is done! and yes it is a fascinating story!
Thanks for your comment.
God bless you!

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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