none
about using substring RRS feed

  • Question

  • how to change the word eg:(sunny day) weather there is a space , to make it become sunny(get rid of words after the space) by using substring(mid function)?
    Tuesday, October 30, 2018 9:06 PM

All replies

  • Hi

    Not using SubString

        Dim s As String = "sunny day"
        ' s = sunny day
    
        s = s.Replace(" day", Nothing)
        ' s = sunny

    using SubString

        Dim s As String = "sunny day"
        ' s = sunny day
    
        s = s.Substring(0, 5)
        ' s = sunny

    NOTE

    Using Replace would work for 'mostly sunny day' and 'occasionally sunny day', but, using SubString would mean you would have to find the length of the part before 'day'.


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, October 30, 2018 9:19 PM
    Tuesday, October 30, 2018 9:18 PM
  • Hi

    mid

     Dim s As String = "sunny day"
            s = (Mid(s, 1, 5))

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 31, 2018 1:59 AM
  • (PS: AIM IS FOR OUTPUT THE FIRST LETTER BY USING FOLLOWING PSEDOCODE)

    PSEDOCODE IS

    SET FLAG TO FALSE(INDICATES IF A SPACE IS PRESENT)

    POSITION TO 0 (WILL SAVE THE POSITION OF SPACE)

    FOR EACH LETTER IN CURRENT STRING

    IF CURRENT CHARACTER IS A SPACE AND FLAG IS FALSE(SPACE HASN'T BEEN FOUND BEFORE)THEN

    SAVE POSITION OF THE SPACE

    SET FLAG TO TRUE(SPACE HAS BEEN FOUND)

    END IF

    END FOR

    IF FLAG IS TRUE THEN(IF A SPACE WAS FOUND)

    OUTPUT SUBSTRING UP TO POSITION OF SPACE

    ELSE

    OUTPUT SUBSTRING UP TO POSITION OF SPACE

    ELSE

    OUTPUT THE FULL STRING

    END IF

    (THX)

    Wednesday, October 31, 2018 9:14 PM
  • MY ORIGINRAL CODE IS:

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim name(973) As String
            Dim rating(973) As Integer
            Dim average As Single
                average = 0
                Call readingfile(name, rating) ' read name and ratings from the file'
                Call caverage(rating, average) 'caluate the average'
                Call displayaverage(average) 'display the average'
                Call search(name, rating) 'linear research,display the name of beaches'
            End Sub
            Private Sub readingfile(ByRef name() As String, ByRef rating() As Integer)
            Dim objTextfile As New System.IO.StreamReader("D:\beachData.csv") 'open the file'
            Dim NewLine As String
                Dim temp(1) As String 'there are 2 pieces of information'
                Dim counter As Integer
            For counter = 0 To 972 'start a fixed loop'
                NewLine = objTextfile.ReadLine()
                temp = NewLine.Split(",") 'seperate'
                name(counter) = temp(0) '1-d array'
                rating(counter) = temp(1)

            Next
            objTextfile.Close() 'close a file'
                objTextfile.Dispose()
            End Sub
            Private Sub caverage(ByRef rating() As Integer, ByRef average As Single)
                Dim counter As Integer
                Dim total As Integer
                Dim beach As Integer
                total = 0 ' set 2 counters'
                beach = 0 'initialise variables'
                For counter = 0 To 971
                    If rating(counter) <> 5 Then '<>means not equal to'
                        total = total + rating(counter)
                        beach = beach + 1
                    End If
                Next
                average = total / beach
            End Sub
            Private Sub displayaverage(ByRef average As Single)

                txtOutput.Text = ("The average is" & average) 'textbox to display the average'
            End Sub
            Private Sub search(ByRef name() As String, ByRef rating() As Integer)
                Dim counter As Integer
                Dim search As Integer
                Dim flag As Boolean
                flag = False
                counter = 0 'initialise the counter'
                search = InputBox("please enter a rating")
                For counter = 0 To 971 'there arer 972 names'
                    If rating(counter) = search Then
                        flag = True ' stop loop until match the search'
                        ListBox2.Items.Add(name(counter) & ".")
                    End If

                Next
            End Sub

        End Class

    Wednesday, October 31, 2018 9:14 PM
  • (PS: AIM IS FOR OUTPUT THE FIRST LETTER BY USING FOLLOWING PSEDOCODE)

    PSEDOCODE IS

    SET FLAG TO FALSE(INDICATES IF A SPACE IS PRESENT)

    POSITION TO 0 (WILL SAVE THE POSITION OF SPACE)

    FOR EACH LETTER IN CURRENT STRING

    IF CURRENT CHARACTER IS A SPACE AND FLAG IS FALSE(SPACE HASN'T BEEN FOUND BEFORE)THEN

    SAVE POSITION OF THE SPACE

    SET FLAG TO TRUE(SPACE HAS BEEN FOUND)

    END IF

    END FOR

    IF FLAG IS TRUE THEN(IF A SPACE WAS FOUND)

    OUTPUT SUBSTRING UP TO POSITION OF SPACE

    ELSE

    OUTPUT SUBSTRING UP TO POSITION OF SPACE

    ELSE

    OUTPUT THE FULL STRING

    END IF

    (THX)

    Hi

    This forum is NOT HERE TO DO SCHOOL WORK for you!


    Regards Les, Livingston, Scotland

    Wednesday, October 31, 2018 9:52 PM
  • If this is not home work then the following can (but not provide an answer) can provide some direction. 

    • Reading a file into a List(Of NameRating) a class to hold all the data from the text file in one line.
    • Once data is loaded you can query it and get things like average and more.
    Imports System.IO
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim results =
                    (
                        From line In File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TextFile1.txt"))
                        Where line.Length > 0
                        Let items = line.Split(","c)
                        Select New NameRating With {.Name = items(0), .Rating = CInt(items(1))}
                    ).ToList
    
    
            For Each nr As NameRating In results
                Console.WriteLine($"{nr.Name,-10}{nr.Rating}")
            Next
    
            Console.WriteLine()
            Console.WriteLine($"Average: {results.Average(Function(item) item.Rating)}")
            Console.WriteLine()
            Console.WriteLine($"Karen's rating: {results.FirstOrDefault(Function(item) item.Name = "Karen").Rating}")
            Console.WriteLine($"Mary's rating: {results.FirstOrDefault(Function(item) item.Name = "Mary").Rating}")
    
            Dim test = results.FirstOrDefault(Function(item) item.Name = "John")
            If test IsNot Nothing Then
                Console.WriteLine($"John's rating: {test}")
            Else
                Console.WriteLine("John not found")
            End If
            Console.WriteLine()
            Dim byRating = results.Where(Function(item) item.Rating = 90).ToList()
            For Each nr As NameRating In byRating
                Console.WriteLine(nr.Name)
            Next
    
    
        End Sub
    End Class
    Public Class NameRating
        Public Property Name() As String
        Public Property Rating() As Integer
    End Class
    

    TextFile1.txt

    Karen,100
    Jim,50
    Mary,90
    Adam,80
    Kim,89
    Bill,90

    Results

    Karen     100
    Jim       50
    Mary      90
    Adam      80
    Kim       89
    Bill      90
    
    Average: 83.1666666666667
    Note average

    We can round

    Console.WriteLine($"Average: {Math.Round(results.Average(Function(item) item.Rating), MidpointRounding.ToEven)}")
    Console.WriteLine($"Average: {Math.Round(results.Average(Function(item) item.Rating), MidpointRounding.AwayFromZero)}")


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, November 1, 2018 1:04 AM
    Moderator