• 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

    • Microsoft makes it easier to find PC specs in Windows 11 Settings by Taras Buria Windows 11 has already received several improvements that make it easier to learn about your computer's specifications. Recently, Microsoft released Spec Cards for the System > About section, which provide basic information about the PC's main components, such as processor, memory, storage, graphics card, and video memory. Now, the Settings app is getting a new way to find your device info. Microsoft wants to display basic device information right on the Home page of the Settings app. The latest preview builds from the Dev and Beta Channels introduced a new "Your device info" card for the Settings' Home page. It displays specs like processor name and speed, graphics card and the amount of video memory, storage, and RAM. The card also has a link to the "About" section, where you will find more information about your computer, its Windows edition, product ID, and the recently introduced FAQ section that answers common hardware-related questions. The "Your device info" card joins the existing cards on the Settings app's home page. While the section offers useful information like quick access to Bluetooth devices, Wi-Fi, personalization, and recommended settings, users received it with mixed reactions, as many considered it another way for Microsoft to promote its services and subscriptions like Microsoft 365, OneDrive, and Game Pass (seriously, who thinks about Game Pass when opening Settings?). Now, the Settings' Home page is a bit more useful, as it saves you a few clicks when checking your computer's specs. If you want to test the new "Your device info" card, update your PC to build 26200.5622 or newer (Dev Channel). Just keep in mind that Microsoft is rolling it out gradually, and it requires signing in with a Microsoft Account in the United States. Other changes in build 26200.5622 include a new Settings section for Quick Machine Recovery, widget improvements, more app recommendations in the "Open with" dialog, and more. Check out the full release notes here.
    • Ponies will finally have good games to play after replaying Last of Us for the 100th time. Oh and I lied, Silent Hill f looks pretty great too, but we already knew about that.
    • China blocks Apple-Alibaba AI venture in retaliation for the US trade war by Hamid Ganji iPhones sold in China, Apple's second biggest market, still lack AI features. While Apple tried to solve the issue by forming an AI venture with China's e-commerce giant Alibaba, the move has faced setbacks from China's regulator, presumably to get back at the US trade war under the Trump administration. According to a new report by Financial Times, citing people familiar with the matter, Apple and Alibaba have been working on their AI venture over the past few months, hoping to bring some AI features to iPhones sold in China. However, the Cyberspace Administration of China hasn't approved the collaboration. Every new iPhone sold worldwide has built-in ChatGPT as a result of the Apple and OpenAI partnership. Since OpenAI has no official presence in China, Apple must partner with local tech companies like Alibaba to offer AI capabilities on iPhones sold in the country. The move could help Apple navigate China's regulatory restrictions, but it's now stalled due to the US-China trade war. The Cyberspace Administration of China doesn't publicly confirm whether halting the Apple-Alibaba AI venture is a response to the US trade war. Still, sources claim this is China's response to the recent tariff clash with the US. China also has a pretty solid record of retaliating against the US reciprocal tariffs. However, the Apple and Alibaba AI partnership also has some opponents in the US. Lawmakers and government officials in Washington have raised concerns about the AI deal. They fear that this collaboration could significantly bolster China's AI capabilities.
    • Raspberry Pi Imager 1.9.4 released bringing performance improvements, bug fixes and more by David Uzondu Raspberry Pi Imager 1.9.4 is now out, marking the first official release in its 1.9.x series. This application, for anyone new to it, is a tool from the Raspberry Pi Foundation. It first came out in March 2020. Its main job is to make getting an operating system onto a microSD card or USB drive for any Raspberry Pi computer super simple, even if you hate the command line. It handles downloading selected OS images and writing them correctly, cutting out several manual steps that used to trip people up, like finding the right image version or using complicated disk utility tools. This version brings solid user interface improvements for a smoother experience, involving internal tweaks that contribute to a more polished feel. Much work went into global accessibility, adding new Korean and Georgian translations. Updates also cover Chinese, German, Spanish, Italian, and many others. Naturally, a good number of bugs got squashed, including a fix for tricky long filename issues on Windows and an issue with the Escape key in the options popup. Changes specific to operating systems are also clear. Windows users get an installer using Inno Setup. Its program files, installer, and uninstaller are now signed for better Windows security. For macOS, .app file naming in .dmg packages is fixed, and building the software is more reliable. Linux users can now hide system drives from the destination list, a great way to prevent accidentally wiping your main computer drives. The Linux AppImage also disables Wayland support by default. The full list of changes is outlined below: Fixed minor errors in Simplified Chinese translation Updated translations for German, Catalan, Spanish, Slovak, Portuguese, Hebrew, Traditional Chinese, Italian, Korean, and Georgian Explicitly added --tree to lsblk to hide partitions from the top-level output CMake now displays the version as v1.9.1 Added support for quiet uninstallation on Windows Applied regex to match SSH public keys during OS customization Updated dependencies: libarchive (3.7.4 → 3.7.7 → 3.8.0) zlib (removed preconfigured header → updated to 1.4.1.1) cURL (8.8 → 8.11.0 → 8.13.0) nghttp2 (updated to 1.65.0) zstd (updated to 1.5.7) xz/liblzma (updated to 5.8.1) Windows-specific updates: Switched to Inno Setup for the installer Added code signing for binaries, installer, and uninstaller Enabled administrator privileges and NSIS removal support Fixed a bug causing incorrect saving of long filenames macOS-specific updates: Fixed .app naming in .dmg packages Improved build reliability and copyright Linux-specific updates: System drives are now hidden in destination popup Wayland support disabled in AppImage General UI/UX improvements: Fixed OptionsPopup not handling the Esc key Improved QML code structure, accessibility, and linting Made options popup modal Split main UI into component files Added a Style singleton and ImCloseButton component Internationalization (i18n): Made "Recommended" OS string translatable Made "gigabytes" translatable Packaging improvements: Custom AppImage build script with Qt detection Custom Qt build script with unprivileged mode Qt 6.9.0 included Dependencies migrated to FetchContent system Build system: CMake version bumped to 3.22 Various improvements and hardening applied Removed "Show password" checkbox in OS customization settings Reverted unneeded changes in long filename size calculation Internal refactoring and performance improvements in download and extract operations Added support for more archive formats via libarchive Lastly, it's worth noting that the system requirements have changed since version 1.9.0: macOS users will need version 11 or later; Windows users, Windows 10 or newer; Ubuntu users, version 22.04 or newer; and Debian users, Bookworm or later.
    • Ancient CD app makes 64-bit comeback to support Windows 11 and probably Windows 10 too by Sayan Sen Remember when CDs or compact discs were a thing? While technically, they still are, their popularity and usage have dropped immensely with the rise in other standards like USB, as the latter continues to evolve, getting faster and gaining more features. Recently, Microsoft enforced some mandatory requirements for USB Type-C so as to ensure a uniform and consistent experience for Windows 11 users. On the topic of Windows 11 and CDs, a CD ripping tool from the Windows 95/98 era, dubbed "CD2WAV32," is back again after 16 years (from the Windows 7 era). The utility has now been updated to work on Windows 11 version 24H2, which is pretty cool. This was not planned, says the author, as they simply wanted to test the app on their newly upgraded Windows 11 PC, but ended up going all the way to make it fully work on Windows 11. Their Windows 11 runs an AMD Ryzen 9600X, 64 GB RAM, and an Nvidia GT 1030 (miswritten as "GT1300"). The developer of the tool notes that they did not run thorough tests on Windows 10, but it works on their Atom-based PC, which is another relic, given how fast technology moves. The author writes (Google-translated from Japanese to English): "From now on, it will only support Windows 11 (24H2). The reason is that this is the only environment the author currently has. I haven't done anything particularly fancy, so I think it will work properly on Windows 10, but I can't guarantee it. All I have left is an ATOM machine that I bought a long time ago that also runs Windows 10, so I've seen that it works lightly on that, but I can't do a detailed test." Atom, for those wondering, was Intel's low-power CPU lineup that it decided to axe back in 2016. The story is similar to how Microsoft gave up on Windows Lumia, as Intel, too, abandoned its mobile chip ambitions once the likes of Qualcomm and MediaTek took over. In terms of the underlying changes, the utility has been compiled now on Delphi 12.1 Community Edition, which is used to make native Windows apps as well as ones for macOS, iOS, and Android. The recent update also brings a significant overhaul in terms of compatibility as well as UX/UI. File sizes and other such metadata are now handled using a 64-bit format instead of the prior 32-bit approach, eliminating overflow issues and ensuring large file and disk space values are displayed correctly. This change is necessary given that large storage volumes are quite common these days. Additionally, support for 16-bit code calling functions has been entirely removed as Windows 11 is 64-bit only; thus, features like MSCDEX and TwinVQ compression are gone. Meanwhile, the font has been changed from MSP Gothic 9pt to Meiryo 10pt, so readability should not be a problem even on 4K screens. In terms of audio file encoding support, it is said to work with MP3 as well as WMA. So, should you download and run it? Probably not, given that the UI is entirely Japanese, but it is still a fun project to look at.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      397
    2. 2
      +FloatingFatMan
      177
    3. 3
      snowy owl
      170
    4. 4
      ATLien_0
      167
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!