Question

Does anybody know how to lowercase the first word for each line in a textbox?

Not the first letter, the first word.

I tried like this but it doesn't work:

For Each iz As String In txtCode.Text.Substring(0, txtCode.Text.IndexOf(" "))
     iz = LCase(iz)
Next
Was it helpful?

Solution 3

The problem with your code is that you are running the loop only on each character of the first word in the whole TextBox text.

This code is looping over each line and takes the first word:

For Each line As String In txtCode.Text.Split(Environment.NewLine)
    line = line.Trim().ToLower()

    If line.IndexOf(" ") > 0 Then
        line = line.Substring(0, line.IndexOf(" ")).Trim()
    End If

    // do something with 'line' here
Next

OTHER TIPS

When you call Substring, it is making a copy of that portion of the string and returning it as a new string object. So, even if you were successfully changing the value of that returned sub-string, it still would not change the original string in the Text property.

However, strings in .NET are immutable reference-types, so when you set iz = ... all you are doing is re-assigning the iz variable to point to yet another new string object. When you set iz, you aren't even touching the value of that copied sub-string to which it previously pointed.

In order to change the value of the text box, you must actually assign a new string value to its Text property, like this:

txtCode.Text = "the new value"

Since that is the case, I would recommend building a new string, using a StringBuilder object, and then, once the modified string is complete, then set the text box's Text property to that new string, for instance:

Dim builder As New StringBuilder()
For Each line As String In txtCode.Text.Split({Environment.NewLine}, StringSplitOptions.None)
    ' Fix case and append line to builder
Next
txtCode.Text = builder.ToString()

The solutions here are interesting but they are ignoring a fundamental tool of .NET: regular expressions. The solution can be written in one expression:

Dim result = Regex.Replace(txtCode.Text, "^\w+",
    Function (match) match.Value.ToLower(), RegexOptions.Multiline)

(This requires the import System.Text.RegularExpressions.)

This solution is likely more efficient than all the other solutions here (It’s definitely more efficient than most), and it’s less code, thus less chance of a bug and easier to understand and to maintain.

Loop through each of the lines of the textbox, splitting all of the words in the line, making sure to .ToLower() the first word:

    Dim strResults As String = String.Empty

    For Each strLine As String In IO.File.ReadAllText("C:\Test\StackFlow.txt").Split(ControlChars.NewLine)

        Dim lstWords As List(Of String) = strLine.Split(" ").ToList()

        If Not lstWords Is Nothing Then

            strResults += lstWords(0).ToLower()

            If lstWords.Count > 1 Then

                For intCursor As Integer = 1 To (lstWords.Count - 1)

                    strResults += " " & lstWords(intCursor)

                Next

            End If

        End If

    Next

I used your ideas guys and i made it up to it like this:

For Each line As String In txtCode.Text.Split(Environment.NewLine)

            Dim abc() As String = line.Split(" ")
            txtCode.Text = txtCode.Text.Replace(abc(0), LCase(abc(0)))

Next

It works like this. Thank you all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top