none
Visual Studio (Visual Basic): Search and return the length of the longest string in a list RRS feed

  • Question

  • Hello,

    I programming as a hobby and currently I make a a string counter in a Windows Form. 

    First: I made a XML file called "Words" with several type of words in different languages:

    <Word English="Car" German="Auto" Spanish= "Coche"/>

    <Word English="Beer" German="Bier" Spanish= "Cerveza"/>

    <Word English="Dog" German="Hund" Spanish= "Perro"/>

    Second: Use a LINQ Query and added all the items to a generic list of string.

    Third: The first textbox show all the items in the list (comma separated):

    Car, Auto, Coche

    Beer, Bier, Cerveza

    Dog, Hund, Perro

    My question is: I want to show the length of the longest English, German and Spanish word in a second textbox. Is this possible?

    In my example it is: 4, 4, 7

    Thanks in advance,

    Jonathan



    • Edited by Jonathan.12 Saturday, May 30, 2020 6:05 PM Mistake in my post
    Saturday, May 30, 2020 3:57 PM

Answers

  • Hi

    You confuse me!  Show the words that match your lengths.

    The first: 

    longest English is Beer which is 4 char in length

    longest German  is any of them as they are all 4 char in length

    longest Spanish is Cerveza which is 7 char in length

    *

    so, according to you question, the result should be 4, 4, 7

    Isn't that correct?


    Regards Les, Livingston, Scotland



    • Edited by leshay Saturday, May 30, 2020 4:42 PM
    • Marked as answer by Jonathan.12 Saturday, May 30, 2020 9:13 PM
    Saturday, May 30, 2020 4:41 PM
  • This will give you length comma delimited into a text box. I

    Dim someList = New List(Of String) From {"Car", "Auto", "Coche"}
    TextBox1.Text = String.Join(",", someList.Select(Function(word) word.Length).ToArray())

    And to get the longest word

            Dim someList = New List(Of String) From {"Car", "Auto", "Coche"}
            Dim longestWord = someList.
                    Aggregate("", Function(max, cur) If(max.Length > cur.Length, max, cur))
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, May 30, 2020 4:45 PM
    Moderator
  • Hi

    Should be straight forward.  See Karens post, or, do something like this

    Starting with the comma separated words in the TextBox.

    Set up variables (say) Eng, Ger and Spa as integers - initialized to -1

    Loop through the lines of the TextBox use a string array to hold a Split of the line (using (","c) as the deliminator. [say - Dim a() As String = line.Split(","c)]

    Check each of a() for length and if longer than the appropriate variable, set the variable to that length.

    Doing this will result in each appropriate variable holding the longest length found for the appropriate language.


    Regards Les, Livingston, Scotland

    • Marked as answer by Jonathan.12 Saturday, May 30, 2020 9:10 PM
    Saturday, May 30, 2020 9:02 PM

All replies

  • Hi

    You confuse me!  Show the words that match your lengths.

    The first: 

    longest English is Beer which is 4 char in length

    longest German  is any of them as they are all 4 char in length

    longest Spanish is Cerveza which is 7 char in length

    *

    so, according to you question, the result should be 4, 4, 7

    Isn't that correct?


    Regards Les, Livingston, Scotland



    • Edited by leshay Saturday, May 30, 2020 4:42 PM
    • Marked as answer by Jonathan.12 Saturday, May 30, 2020 9:13 PM
    Saturday, May 30, 2020 4:41 PM
  • This will give you length comma delimited into a text box. I

    Dim someList = New List(Of String) From {"Car", "Auto", "Coche"}
    TextBox1.Text = String.Join(",", someList.Select(Function(word) word.Length).ToArray())

    And to get the longest word

            Dim someList = New List(Of String) From {"Car", "Auto", "Coche"}
            Dim longestWord = someList.
                    Aggregate("", Function(max, cur) If(max.Length > cur.Length, max, cur))
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, May 30, 2020 4:45 PM
    Moderator
  • Did your teacher give any more conditions you must fullfil

    I don't see any situation where this can be in real live beside a school assignment. 

    I hope nobody gives you a solution. 

    Be aware, there is no problem if you show your solution and decent persons tell you what you did wrong. 

    Giving you a solution is the really bad and can only done by those for who see cheating as normal.


    Success
    Cor

    Saturday, May 30, 2020 7:44 PM
  • Hi

    Should be straight forward.  See Karens post, or, do something like this

    Starting with the comma separated words in the TextBox.

    Set up variables (say) Eng, Ger and Spa as integers - initialized to -1

    Loop through the lines of the TextBox use a string array to hold a Split of the line (using (","c) as the deliminator. [say - Dim a() As String = line.Split(","c)]

    Check each of a() for length and if longer than the appropriate variable, set the variable to that length.

    Doing this will result in each appropriate variable holding the longest length found for the appropriate language.


    Regards Les, Livingston, Scotland

    • Marked as answer by Jonathan.12 Saturday, May 30, 2020 9:10 PM
    Saturday, May 30, 2020 9:02 PM
  • Hi

    Should be straight forward.  See Karens post, or, do something like this

    Starting with the comma separated words in the TextBox.

    Set up variables (say) Eng, Ger and Spa as integers - initialized to -1

    Loop through the lines of the TextBox use a string array to hold a Split of the line (using (","c) as the deliminator. [say - Dim a() As String = line.Split(","c)]

    Check each of a() for length and if longer than the appropriate variable, set the variable to that length.

    Doing this will result in each appropriate variable holding the longest length found for the appropriate language.


    Regards Les, Livingston, Scotland


    Thank you for the reply. I can use your advice for writing my code. Thank you very much.
    My problem is solved.
    Saturday, May 30, 2020 9:13 PM