I am writing a program where I need to capitalize the first letter of each word in a text file. Example of information in the text file would be:

this is the first line
this is the second line
this is the third line

I have written a code that gives me back the following information:

This Is The First Line
this Is The Second Line
this Is The Third Line

I cannot figure out how to get the first word in the second and third lines capitalized.

I have used a split separator to create the array, the separator is a space. I know it is not capitaizing those letters because there is no space at the beginning of the line, but have no idea what to do to fix the problem. Can someone please help me? Thank you so much

I forgot to mention that the information from the text file is going into the textbox, not another file.

Member Avatar for iamthwee

In that case after you have capitalised the first letter of the line write it to the text box.

Did you want an example? Let me just fire up my windows box.

Yes, could you please give me an example. Thank you

Member Avatar for iamthwee

Oopsie sorry about that. I was on my linux box :)

Imports System.IO

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim objStreamReader As StreamReader
        Dim strLine As String

        'Pass the file path and the file name to the StreamReader constructor.
        objStreamReader = New StreamReader("C:\foo.txt")

        'Read the first line of text.
        strLine = objStreamReader.ReadLine

        'Continue to read until you reach the end of the file.
        Do While Not strLine Is Nothing

            Dim bar As String
            If (strLine.Length > 0) Then

                bar = strLine.Substring(0, 1).ToUpper & strLine.Substring(1, strLine.Length - 1)

                'Write the line to the textbox.
                TextBox1.Text = TextBox1.Text & bar & System.Environment.NewLine
            End If
            'Read the next line.
            strLine = objStreamReader.ReadLine
        Loop

        'Close the file.
        objStreamReader.Close()

        Console.ReadLine()

    End Sub
End Class

You probably have to select the multiline option on the text box...

Member Avatar for iamthwee

I have just edited the code so it is more robust when it comes to potential errors (such as blank lines in the file). You may need to refresh your browser to see it take effect.

Thank you so much I will try it.

Member Avatar for iamthwee

let us know how you get on :)

It worked except now it will not capitalize the first letter of every word, it only capitalizes the first word of every line.

Here is what my original code was:

Private Sub xCapitializeButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCapitializeButton.Click
' ***capitalize all word in text file.
Dim aString As StreamReader = My.Computer.FileSystem.ReadAllText("input.txt")
Dim stringReturn As String
If My.Computer.FileSystem.FileExists("input.txt") Then
Me.xInputTextBox.Text = (Convert.ToString(aString.ToLower))
End If

Do While (aString.IndexOf(Space(2)) >= 0)
aString = aString.Replace(Space(2), Space(1))
Loop

Dim capArray() As String = (aString.Split(" "))

For I As Integer = 0 To capArray.Length - 1

If capArray(I).Length = 1 Then
capArray(I) = capArray(I).ToUpper
Else ' ***join them back together
capArray(I) = capArray(I).Substring(0, 1).ToUpper & capArray(I).Substring(1).ToLower
End If

Next I

stringReturn = String.Join(" ", capArray)
Me.xInputTextBox.Text = (stringReturn.ToString)

End Sub


Maybe you can show me what I am doing wrong here.

The input files has this info:

this is the first line
this is the second line
this is the third line

I need it to return this:

This Is The First Line
This Is The Second Line
This Is The Third Line

But it returns this:

This Is The First Line
this Is The Second Line
this Is The Third Line

Please take a look at let me know. Thanks so much.

Member Avatar for iamthwee

right.

A few things here. Your words can be separated by either multiple spaces, tabs newlines etc. So it can become quite tricky to identify what is a "word". The easiest way to do this would be to use regex and split with (something like) :

System.Text.RegularExpressions.Regex.split(TextBox1.Text, "\w+").

Also the way I was appending to a text box was very slow. Using a listbox or writing the string to the textbox just once at the end would be a better way.

I'll show you some code later.

Member Avatar for iamthwee
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim objStreamReader As StreamReader
        Dim strLine As String

        'Pass the file path and the file name to the StreamReader constructor.
        objStreamReader = New StreamReader("C:\foo.txt")

        'Read the first line of text.
        strLine = objStreamReader.ReadLine

        'Continue to read until you reach the end of the file.
        Do While Not strLine Is Nothing

            'Write the line to the textbox.
            RichTextBox1.Text = RichTextBox1.Text & strLine & System.Environment.NewLine

            'Read the next line.
            strLine = objStreamReader.ReadLine
        Loop

        'Close the file.
        objStreamReader.Close()

        Console.ReadLine()

        Dim tmpText As String = RichTextBox1.Text

        'This splits the text into all whitespace \n \r \t etc
        Dim strArray() As String = System.Text.RegularExpressions.Regex.Split(RichTextBox1.Text, "\s+")

        'This splits the text by NON-WHITE space.
        Dim wordArray() As String = System.Text.RegularExpressions.Regex.Split(RichTextBox1.Text, "\w+")

        'Loop through the array

        Dim m As Integer = 0

        Dim wah As String = ""

        Dim t As String
        For Each t In strArray
            wah = wah & wordArray(m) & (myCapital(t))
            m = m + 1
        Next t

        RichTextBox2.Text = wah

    End Sub

    'This function capitalises a string
    Function myCapital(ByVal t As String) As String
        Dim bar As String = ""
        If t.Length > 0 Then
            bar = t.Substring(0, 1).ToUpper & t.Substring(1, t.Length - 1)
        End If
        Return bar
    End Function
End Class

This works on newlines, tabs and multiple spaces. There is a little bug, if the text file starts with a few spaces.

Thanks so much. Alot of what you wrote above am I not familiar with yet. I appreciate your help very much.

Member Avatar for iamthwee

Also you should change

'This splits the text by NON-WHITE space.        Dim wordArray() As String = System.Text.RegularExpressions.Regex.Split(RichTextBox1.Text, "\w+")

To...

'This splits the text by NON-WHITE space.        Dim wordArray() As String = System.Text.RegularExpressions.Regex.Split(RichTextBox1.Text, "\S+")

If your text file contains fullstops or commas. Something I spotted today. There may be other errors I haven't noticed. Anyway good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.