• 0

String Test in VB.NET


Question

I am suppose to create an application that displays the first letter, last letter, and middle letter of a word or phrase. This is my code so far:

'Jane Lewis

'String Test

'Displays the first letter, last letter, and middle letter of a word or phrase

Public Class frmStringTest

Private Sub btnDisplayData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayData.Click

Dim word As String

Dim firstLetter As String

Dim secondLetter As String

Dim lastLetter As String

Dim numLetters As Integer

?

word = Me.txtPhrase.Text

word = word.ToLower

numLetters = word.Length

End Sub

End Class

 

What else do I need?

We are working on Strings this week, and I am not understanding Strings very well, and they are confusing to me. I ask my teacher for an one on one explanation, but my Programming teacher didn't make it any clearer.If you give me the for this problem, can you explain why you do this and that, please. Thank you.

 

Link to comment
https://www.neowin.net/forum/topic/1203365-string-test-in-vbnet/
Share on other sites

15 answers to this question

Recommended Posts

  • 0

http://msdn.microsoft.com/en-us/library/system.string(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

 

Take a look at the "Properties" section for something that might help. It sounds like you want the character at the first position in the String as well as the last position and somewhere halfway...

  • 0
  On 04/03/2014 at 18:31, Jane Lewis said:

I am suppose to create an application that displays the first letter, last letter, and middle letter of a word or phrase. This is my code so far:

'Jane Lewis

'String Test

'Displays the first letter, last letter, and middle letter of a word or phrase

Public Class frmStringTest

Private Sub btnDisplayData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayData.Click

Dim word As String

Dim firstLetter As String

Dim secondLetter As String

Dim lastLetter As String

Dim numLetters As Integer

?

word = Me.txtPhrase.Text

word = word.ToLower

numLetters = word.Length

End Sub

End Class

 

What else do I need?

We are working on Strings this week, and I am not understanding Strings very well, and they are confusing to me. I ask my teacher for an one on one explanation, but my Programming teacher didn't make it any clearer.If you give me the for this problem, can you explain why you do this and that, please. Thank you.

If you look at a string as a list of chars, you just need the first character which starts at 0 then the last, given to you by word.Length, and then the middle which would be word.Length divided by 2.  

 

http://msdn.microsoft.com/en-us/library/hxthx5h6(v=vs.110).aspx

  • 0
  On 04/03/2014 at 18:31, Jane Lewis said:

I am suppose to create an application that displays the first letter, last letter, and middle letter of a word or phrase. This is my code so far:

'Jane Lewis

'String Test
'Displays the first letter, last letter, and middle letter of a word or phrase
Public Class frmStringTest
Private Sub btnDisplayData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayData.Click
Dim word As String
Dim firstLetter As String
Dim secondLetter As String
Dim lastLetter As String
Dim numLetters As Integer
?
word = Me.txtPhrase.Text
word = word.ToLower
numLetters = word.Length
End Sub
End Class

What else do I need?

We are working on Strings this week, and I am not understanding Strings very well, and they are confusing to me. I ask my teacher for an one on one explanation, but my Programming teacher didn't make it any clearer.If you give me the for this problem, can you explain why you do this and that, please. Thank you.

 

You should read up on strings.

Now there are several approaches to this.. I would recommend this way:

Public Class Form1

    Dim myString As String
    Dim stringLen As Integer
    Dim firstChar As String
    Dim lastChar As String
    Dim middleLetter As String
    Dim saveMYVars As Boolean
    Dim anothervar As Double
    Dim learntocode As String

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

        myString = "Firey is the best coder ever"

        'step 1, get the string length
        Dim counter As Integer
        For counter = 0 To myString.Length
            stringLen = stringLen + 1
        Next counter

        'step 2, find the first letter
        firstChar = myString.Substring(0, 1)

        'step 3 last letter
        lastChar = myString.Substring(stringLen - 2, 1)

        'step 4, ensure we have the right letters
        firstChar = myString(0)
        lastChar = myString(stringLen - 2)

        'Step 5 get the middle letter.
        For counter = 1 To myString.Length / 2
            middleLetter = myString.Substring(counter - 1, 1)
        Next counter

        'Step 6, verify it's the middle letter
        middleLetter = myString(stringLen / 2)

        'Step 7, save the variables off so the computer doesn't forget
        saveMYVars = True 'this is critical in any VB Program

        'step 8, show it
        If (saveMYVars) Then
            MessageBox.Show("Firster")
            MessageBox.Show(myString(0))
            MessageBox.Show("Middleer")
            MessageBox.Show(myString.Substring(counter - 1, 1))
            MessageBox.Show("Laster")
            MessageBox.Show(lastChar)
        End If
    End Sub
End Class

The code above is tested and working.

  • 0
  On 04/03/2014 at 18:56, firey said:

You should read up on strings.

Now there are several approaches to this.. I would recommend this way:

Public Class Form1

    Dim myString As String
    Dim stringLen As Integer
    Dim firstChar As String
    Dim lastChar As String
    Dim middleLetter As String
    Dim saveMYVars As Boolean
    Dim anothervar As Double
    Dim learntocode As String

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

        myString = "Firey is the best coder ever"

        'step 1, get the string length
        Dim counter As Integer
        For counter = 0 To myString.Length
            stringLen = stringLen + 1
        Next counter

        'step 2, find the first letter
        firstChar = myString.Substring(0, 1)

        'step 3 last letter
        lastChar = myString.Substring(stringLen - 2, 1)

        'step 4, ensure we have the right letters
        firstChar = myString(0)
        lastChar = myString(stringLen - 2)

        'Step 5 get the middle letter.
        For counter = 1 To myString.Length / 2
            middleLetter = myString.Substring(counter - 1, 1)
        Next counter

        'Step 6, verify it's the middle letter
        middleLetter = myString(stringLen / 2)

        'Step 7, save the variables off so the computer doesn't forget
        saveMYVars = True 'this is critical in any VB Program

        'step 8, show it
        If (saveMYVars) Then
            MessageBox.Show("Firster")
            MessageBox.Show(myString(0))
            MessageBox.Show("Middleer")
            MessageBox.Show(myString.Substring(counter - 1, 1))
            MessageBox.Show("Laster")
            MessageBox.Show(lastChar)
        End If
    End Sub
End Class

The code above is tested and working.

May I ask why you set counter = 0

And I am a visual learner I have to see it done, and then I will get it.

  • 0

To be completely honest with you, my code is crazy convoluted (to get you thinking about what is happening).  You can do your entire project in 4 lines with only 1 variable. 

Think of strings as an array of chars.  You access arrays using indexes.

Index 0 = first
Index (length - 1) = last
Index (length / 2 - 1) = middle.

Imagine your string is an array (I show examples of it in my code).  Use what I wrote above (substituting length for that actual string length) to achieve what you want.

If I was to write the code in C# it would be something simple ie)

string myString = "myString";
MessageBox.Show("Firster: " + myString[0].toString() + " Middle Letter: " + myString[(myString.Length / 2) - 1].ToString() + " Last Letter: " + myString[myString.Length - 1],"Parts of a string"); 

  • 0
  On 04/03/2014 at 19:11, snaphat (Myles Landwehr) said:

You guy shouldn't be giving outright solutions. This is a homework assignment...

I agree, hence my code is so convoluted no teacher would ever accept it.  It is also 4 different ways of solving it with random ###### that does nothing.

  • 0
  On 04/03/2014 at 19:08, firey said:

To be completely honest with you, my code is crazy convoluted (to get you thinking about what is happening).  You can do your entire project in 4 lines with only 1 variable. 

Think of strings as an array of chars.  You access arrays using indexes.

Index 0 = first

Index (length - 1) = last

Index (length / 2 - 1) = middle.

Imagine your string is an array (I show examples of it in my code).  Use what I wrote above (substituting length for that actual string length) to achieve what you want.

If I was to write the code in C# it would be something simple ie)

string myString = "myString";

MessageBox.Show("Firster: " + myString[0].toString() + " Middle Letter: " + myString[(myString.Length / 2) - 1].ToString() + " Last Letter: " + myString[myString.Length - 1],"Parts of a string"); 

I think I got a better understanding of Strings now, thank you. You should be a teacher yourself.

  • 0
  On 04/03/2014 at 19:15, firey said:

I agree, hence my code is so convoluted no teacher would ever accept it.  It is also 4 different ways of solving it with random #### that does nothing.

 

  On 04/03/2014 at 19:08, firey said:

To be completely honest with you, my code is crazy convoluted (to get you thinking about what is happening).  You can do your entire project in 4 lines with only 1 variable. 

Think of strings as an array of chars.  You access arrays using indexes.

Index 0 = first

Index (length - 1) = last

Index (length / 2 - 1) = middle.

Imagine your string is an array (I show examples of it in my code).  Use what I wrote above (substituting length for that actual string length) to achieve what you want.

If I was to write the code in C# it would be something simple ie)

string myString = "myString";

MessageBox.Show("Firster: " + myString[0].toString() + " Middle Letter: " + myString[(myString.Length / 2) - 1].ToString() + " Last Letter: " + myString[myString.Length - 1],"Parts of a string"); 

This is the final code I have:

Public Class frmStringTest

Private Sub btnDisplayData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplayData.Click

Dim myString As String = Me.txtPhrase.Text 'declares the phrase enter by the user as a string

Dim stringLen As Integer 'declares as a string

Dim firstletter As String 'declares as a string

Dim lastletter As String 'declares as a string

Dim middleLetter As String 'declares as a string

Dim counter As Integer 'decalres counter as an Integer

For counter = 0 To myString.Length 'The counter starts at O to how many characters/letters are in the word/phrase

stringLen = stringLen + 1 'Goes up by 1

Next counter

'finding the first letter

firstletter = myString.Substring(0, 1)

Me.lblFirstAnswer.Text = firstletter 'Letter/character for the first letter will display here

'Finding the last letter

lastletter = myString.Substring(stringLen - 2, 1)

Me.lblLastAnswer.Text = lastletter 'Letter/character for the last letter will display here

'Ensures that I have the right letters

firstletter = myString(0)

lastletter = myString(stringLen - 2)

'Finding the middle letter.

For counter = 1 To myString.Length / 2

middleLetter = myString.Substring(counter - 1, 1)

Next counter

'Step 6, verify it's the middle letter

middleLetter = myString(stringLen / 2 - 1)

Me.lblMiddleAnswer.Text = middleLetter

End Sub

End Class

  • 0

Hello,

  On 04/03/2014 at 19:02, Jane Lewis said:

May I ask why you set counter = 0

And I am a visual learner I have to see it done, and then I will get it.

We already gave you that once. You should know some basic VB .NET functions a month later.

  On 04/03/2014 at 19:11, snaphat (Myles Landwehr) said:

You guy shouldn't be giving outright solutions. This is a homework assignment...

Once, IMO, OK. Twice, no. But then I guess firey (nothing against you firey :) ) is allowed to give 90% of the solution instead of 100% :huh:

Anyways, looking at this: http://msdn.microsoft.com/en-us/library/hxthx5h6%28v=vs.110%29.aspx should be MORE than enough to help you out.

  • 0
  On 11/03/2014 at 16:20, riahc3 said:

Hello,

We already gave you that once. You should know some basic VB .NET functions a month later.

Once, IMO, OK. Twice, no. But then I guess firey (nothing against you firey :) ) is allowed to give 90% of the solution instead of 100% :huh:

Anyways, looking at this: http://msdn.microsoft.com/en-us/library/hxthx5h6%28v=vs.110%29.aspx should be MORE than enough to help you out.

I didn't realize this was the second post.  However yea I am not going to give a 100% answer.  People do need to learn for themselves and I agree fully.   At the same time though, simply redirecting people to another resource doesn't do much especially when you don't know wtf you are looking at.

So I figured I'd give some ridiculous answer, and she could go through with a deubgger and test each line and see what happens.

  • 0

Hello,

  On 11/03/2014 at 16:25, firey said:

At the same time though, simply redirecting people to another resource doesn't do much especially when you don't know wtf you are looking at.

A month later the person should already know a basic function in VB .NET

I understand at first functions can surprise you but a month later......Basic things like Substring are not really hard.

http://lmgtfy.com/?q=how+to+use+substring+function+in+vb+.net

Just looking at Google pretty much lays it down.

  • 0
  On 11/03/2014 at 16:29, riahc3 said:

Hello,

A month later the person should already know a basic function in VB .NET

I understand at first functions can surprise you but a month later......Basic things like Substring are not really hard.

http://lmgtfy.com/?q=how+to+use+substring+function+in+vb+.net

Just looking at Google pretty much lays it down.

As I say, I didn't realize that this was already asked a month ago by the same person.  You are right, basic things aren't hard and for the most part are easy to grasp.  

  • 0

Hello,

  On 11/03/2014 at 16:32, firey said:

As I say, I didn't realize that this was already asked a month ago by the same person.  You are right, basic things aren't hard and for the most part are easy to grasp.

The only reason I came into this thread is because I was in that thread so its normal you didnt notice :)

Also, he "left" the previous thread; No solved, no thanks, nothing.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • With Sony basically owning EVO, this device isn't that big of a surprise. Though I'm sure the price tag will be, we'll see.
    • wtf are you talking about... kinda ironic of you saying it looks dumb because you clearly have problems with your eyes. I get that you misread FIGHT as FLIGHT but how can you look at the actual device and still think it has anything to do with planes... have you never seen an arcade cabinet?
    • I don't usually complain about articles here, but this is just ridiculous, you couldn't ask your precious AI to write something about tech instead? This literally has nothing to do with tech.
    • Microsoft's "Athena" AI: A blueprint for your own dev team's productivity boost by Paul Hill Last month, Microsoft made many developer-oriented announcements at its annual BUILD conference. One of the tools that it announced at the time was called Athena, an artificial intelligence agent that lives in Microsoft Teams and aims to speed up product development processes. Now, however, Microsoft has released the blueprint of Athena so anyone can begin implementing a similar helper for their work. Rather than just another chatbot, Athena is a deeply integrated agent accessible through Teams that helps to connect people, tools, and data throughout the product development process. Athena is smart enough to work out what needs to happen next and helps team members get it done without having to go to different applications - Athena controls it all from Teams and you just have to communicate in natural language. Athena can be embedded in several developer workflows including Teams, Azure DevOps, and GitHub. Perhaps the greatest thing about Athena is that it’s not a new product being sold by Microsoft, but rather a methodology and open-source template (Dex) that organizations or individuals can take to build their own AI agents, allowing for more customization. To get started with Athena, you'll want to set up the Dex agent. Microsoft has also published a breakout session video about Athena so you can take a deeper dive. Who it affects, and how The primary beneficiaries of Microsoft’s decision to release Athena open source are engineering teams everywhere who will be freed from boring, repetitive tasks such as pull request (PR) reviews, work item management, and security checks. This will let them get on with coding new features and innovating - something that Microsoft has been pushing hard for since the start of the latest AI revolution. For product managers and engineering leaders, Athena also looks set to be incredibly useful as they will get better real-time visibility into the status of projects, if a release is truly ready for launch, and to ensure the team is aligned. Organizations from small to large will be able to benefit from using Athena. Due to its open-source nature, Athena can be tailored to meet specific development processes. This could unlock faster delivery cycles and improve code quality across the board. Why it's happening Athena is already being used internally at Microsoft by over 2,000 of its engineers. The Redmond giant explained that this has led to “measurable gains” in speed, quality, and focus. Aside from faster review cycles for developers, Athena is also surfacing release-blocking bugs earlier, enabling the consistent completion of security and privacy workflows, and providing quicker health assessments so that teams can gauge the overall health of their software delivery. By handling all these more boring tasks, Athena can free up developers to build more features into their projects. It also has the potential to speed up delivery times so that end users can use the new software faster, and with potentially less bugs. We often hear the term “democratization” in tech, a process that makes technologies more accessible and affordable. According to this definition, Microsoft’s release of Athena delivers on democratizing AI for developers as its open source and allows people to integrate AI Into their workflows, without starting from scratch. The move also aligns with Microsoft's AI strategy, that is, putting it all throughout its products. Copilot is probably the most notorious for its omnipresence in essentially every Microsoft product including Windows and Edge. Unlike Copilot, developers get a bit more freedom with Athena, but it’s still tied up with Microsoft products, namely Teams and GitHub. Caveats and what to watch for While it’s great that Microsoft is making its Athena blueprint accessible, one issue is that developers may still find it a bit complex to implement as there are still specific customizations organizations will want to make. Additionally, this solution involves a more involved setup process as outlined in the GitHub README. Another thing organizations should be wary about is data privacy and security implications when it comes to integrating with sensitive internal systems. Organizations that are working on secretive projects probably wouldn’t want to use Athena as this could put sensitive code in the hands of third parties. It’s not only technical issues that need considering either, there is also the human element. Some people may have concerns about AI hallucinating or ethical concerns around job security that could hurt adoption. To this end, Microsoft has reaffirmed that Athena is supposed to assist teams only, not replace team members. While Athena can be extremely useful, as shown by the results internally at Microsoft, human oversight and judgment will still be vital. Complex decision and creative problem-solving in development are some areas where a human still needs to be involved. Source: Microsoft
  • Recent Achievements

    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
    • Apprentice
      DarkShrunken went up a rank
      Apprentice
    • Dedicated
      CHUNWEI earned a badge
      Dedicated
    • Collaborator
      DarkShrunken earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      383
    2. 2
      +FloatingFatMan
      176
    3. 3
      ATLien_0
      169
    4. 4
      snowy owl
      169
    5. 5
      Xenon
      133
  • Tell a friend

    Love Neowin? Tell a friend!