Greenhorn Posted March 15, 2011 Posted March 15, 2011 Hey guys, new to the forum as of today and am looking forward to learning a lot from you all. Currently I'm working on a VB basic form for my college programming class. One of the requirements is to display the text in a list box in reverse order and vertically. I have this for the reversal of the text: lstbxItemsPurchased.Items.Add(StrReverse(txtItemPurch.Text)) However, I have no idea how to make characters displayed vertically and seperately from each other in terms of the line they are one. Here's a sample but I want this to work regardless of the number of characters entered. "laptop computer" should become: r e t u p m o c p o t p a l Thanks in advance guys Quote
orca44 Posted March 16, 2011 Posted March 16, 2011 Welcome to the forum. :) Since each letter should be displayed on a seperate line, there should be a .Add() call for each letter. So, you have to loop through the string from front to back, or back to front and add each letter to the listbox seperately. Quote
Leaders snarfblam Posted March 16, 2011 Leaders Posted March 16, 2011 It sounds to me like the purpose of this excersise is to learn and practice loops. To that end, StrReverse is not the correct approach. That would be like a student using a calculator on a long division test. If you're looking to do the reversing yourself, here are my thoughts. You can get individual characters from a string using the Chars property. (Since Chars is the "default property", you could also just use parens.) Dim SomeString As String = "ABCDEFG" [color="Green"]' Get the third character of a string[/color] MessageBox.Show(SomeString.Chars(2)) [color="Green"]' This is equivalent and produces identical results.[/color] MessageBox.Show(SomeString(2)) You can reverse a string with a loop that goes from the last character to the first, taking each character and appending it to a variable. Remember that the character indecies start at zero, so the last character would be one less than the length of the string: Dim LastIndex As Integer = SomeString.Length - 1 To run a loop backwards, you need to specify a negative step. [color="Green"]' A value of -1 is added to i each iteration, until endValue is reached[/color] For i As Integer = startValue To endValue [color="Blue"]Step - 1[/color] And, just to cover all my bases, you use the & symbol to combine strings. Dim ABCD As String = "ABCD" Dim DCBA As String = ABCD(3) [color="Green"]'D'[/color] DCBA = DCBA & ABCD(2) [color="Green"]'D' & 'C' -> 'DC'[/color] DCBA = DCBA & ABCD(1) [color="Green"]'DC' & 'B' -> 'DCB'[/color] DCBA = DCBA & ABCD(0) [color="Green"]'DCB' & 'A' -> 'DCBA'[/color] That's everything you would need to reverse a string if StrReverse did not exist. Quote [sIGPIC]e[/sIGPIC]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.