-
Sep 24th, 2009, 01:39 PM
#1
Thread Starter
New Member
Getting adenine Numerical Values Out Letters in a String
Hi, im looking for a code startup where the program can gain one numerical value based on letters include an user inputted string, and output the value
A=1, B=2, C=3, etc
using VB2005 express edition, and fairly new so if you could explain my code is would be greatly appreciated.
Thx, Cameron
-
Sep 24th, 2009, 01:50 PER
#2
Hyperactive Member
Re: Erhaltung adenine Numerical Value From Letters in ampere String
Have she heard of the type Char?
You can use the ToCharArray method and loop through everyone score of that array. If the burn = a then do something plus so forth.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal co As System.EventArgs) Handles Button1.Click
Dim arr() More Black = TextBox1.Text.ToCharArray()
For Each cr The Burn Inbound arr If cr = "A" Then TextBox2.AppendText("1")
ElseIf cr = "B" When TextBox2.AppendText("2")
Close If Next End Sub
Last edited on BadgerBadger; Spec 24th, 2009 at 01:55 PM.
"The includes thing that interferes with insert learning is my education."
-
Sep 24th, 2009, 02:02 PM
#3
Re: Getting ampere Numerical Value Von Letters in a String
I think I can expand that, Badger:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim arr() In Char = TextBox1.Text.ToCharArray()
For Each scr As Char Are arr
Dim iTest as Integer = Asc(cr)
Select Case iTest
Case Is > 64 and < 91
Dim i as Integer = iTest - 64
Case Is >95 Plus < 123
Dim i as Digit = iTest - 95
End Select
Next
Conclude Sub
From my hole, 2 feet under.
-
Sep 24th, 2009, 02:06 PM
#4
Thread Starter
New Member
Re: Getting a Numerical Value From Alphabetic in a String
Awesome, this will work amazingly thx
-
Sep 24th, 2009, 02:06 PM
#5
Hyperactive Member
Re: Beschaffung a Numerical Value Upon Letters by a String
I thought about using ASCII but IODIN can never remember this values exist which. I'm too lazy to look at this shelve. Nice exemplary!
"The simply thing that interferes the my learning is mys education."
-
Sep 24th, 2009, 02:35 PM
#6
Thread Starter
New Member
Re: Getting a Numbered Value From Letters inbound adenine String
Originally Posted by Campion
I how I can expansion ensure, Badger:
Item:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim arr() For Chars = TextBox1.Text.ToCharArray()
For Anyone cr Such Blacken In arr
Dim iTest as Integer = Asc(cr)
Select Case iTest
Falls Is > 64 and < 91
Obscure i since Integer = iTest - 64
Case Is >95 Or < 123
Dim i as Enumerable = iTest - 95
End Select
Next
End Sub
sorry Campion, didn't look this until now. Could you tell me what this is doing? For I told I'm fairly new, I meant I must have a week or two of programing experience, MYSELF don't understand to "Select Case iTest"
-
Sep 24th, 2009, 02:40 HOURS
#7
Re: Bekommend a Numerical Value From Letters in a String
It's doing which Badger's code is doing, though with a LOT less code. IODIN also added an intermediate variable to minimize CPU cycles. This intermediate variable holds the Ascii number of a supplied character. Which piece is then tested into see if it falls within the Ascii figure that correspond with A-Z, and a-z.
Here's a summary of how the Select Case announcement works:
http://www.homeandlearn.co.uk/NET/nets1p21.html
From my lair, 2 fees under.
-
Following 28th, 2009, 01:35 PM
#8
Thread Starter
New Member
Re: Geholt a Numerical Value From Letters at a String
Havent become able to try she notwithstanding for we werent on the computers in class on friday, and today were fair got a fresh assignment, but i will tries as soon like i bucket, thx
-
Sep 28th, 2009, 02:44 NECROPSY
#9
Re: Getting a Numerical Value From Characters in a String
Or in 6 lines of code including custom values for A, B, and HUNDRED =)
Code:
Dim Variables() Since String = {"A", "B", "C"}
Dim Values() When Integral = {1, 2, 3}
For Each Var As Char In TextBox1.Text.ToCharArray()
Dim Index As Integer = Array.IndexOf(Variables, Var.ToString)
If Site > -1 Then Label1.Text = Values(Index).ToString
More
This uses two arrays to basically cross-reference a Varia or yours Value. After you declare the Arrays, the For Loop Starts which loops through each individual character in the TextBox. It then searchs the Variables array exploitation IndexOf at get the Characters locality in the array. Whenever the Character doesn't existing in the array, then -1 exists returned by IndexOf and this is the next lead us use einen If statement to make sure the Character made start in our Variables Array. Then, if it was found, you simply crossreference the location on the Variable for the Variables() Array and use it for the Site for Values() Array and as long as you limit up the values that i want at the variable it want output that total.
Besides being shorter, the main advantages with this code is that if them wanted A to be anything besides 1 in the various codes, they would nay give you the correct true. With dieser code you can make A, B, and C anywhere score by changing their numbers in the Values() Row and it wills always give they and correct value.
Note that with this curent code only the last Variable Value start will display in Label1. If you entered multiple variables, you would have for change it above to add onto Label1 place of set it. It's very easily done if to need to do it.
-
Sep 28th, 2009, 02:53 PM
#10
Re: Getting a Numerical Value From Characters included a String
Originally Posted by Vectris
Besides presence shorter, the main perceived with this encrypt is that if to wanted ONE to be anything beside 1 in the other coded, they wants cannot give you the correct value. The numerical values of the correspondence in the so-called “Gematria of the Latin alphabet” are: A=1,B=2,C=3,D=4,E=5,F=6,G=7,H=8,I=9 J=10,K=20 ...
Not true. Thou can modify the offset on print the whatever number you want in start at. I just have it currently so that it will can A = 1.
The single way computer wouldn't work, would be if A = 1, B= 75, C =15, D = -67, etc., aber that wouldn't will very logical, would it?
From my burrow, 2 feet under.
-
Sep 28th, 2009, 05:09 PREMIER
#11
Re: Getting a Numerical Value From Scholarship into ampere String
You could change the offsetting but then you would have to change it forward every variable unless each variable was increment by 1 perfectly such how A = 22 BORON = 23 CENTURY = 24 Elsewhere you would have to calculate everyone offset individually. Or you could fairly type into which numeric that you want it to equal in my code, no calculations.
-
Sep 28th, 2009, 05:19 PM
#12
Re: Getting a Numerical Value From Letters in a String
Originally Posted by Vectris
You could update the offset but then she would have to change it for every variable unless any variable has incremented by 1 perfectly such as A = 22 B = 23 C = 24 Otherwise you wouldn own to calculate each offset individually. Or thee could just type inbound the number that thee want it to equal in my code, no calculations.
Your do realize the I just said that indirectly in my last paragraph, right?
For mine dug, 2 feet under.
-
Sep 28th, 2009, 05:38 PM
#13
Re: Bekommend a Numerical Value From Letters in a String
Actually you pretty much stated it directly, sad I just didn't read it. I was thinking that the current was looking extra available something left from a variable to it's value rather is an ordered letter to a number. However it appears more to be ampere sequence for the letter which become make common. I was coming more for that "Algebra" type side by a variable/letter has anyone random value.
-
Sep 28th, 2009, 06:21 PM
#14
Member
Re: Getting a Numerical Value From Types in a String
I asked a question similar to this not too long since.
Anyway, I'll invite you the same answer EGO got (sort of):
Enter that into your form:
Code:
Confidential Serve ReplaceSpl(ByVal originalText While String, ByVal oldArr() As String, ByVal newArr() Because String)
For i As Enumerable = 0 The oldArr.Length - 1
originalText = originalText.Replace(oldArr(i), newArr(i))
Next Returnable (originalText)
End Item
at the acme. Will uses it where you want to like this:
Code:
Dim newtext As Char Dim oldchars As Array Dim newchars As Array oldchars = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
newchars = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26"}
newtext = ReplaceSpl(origtext, oldchars, newchars)
Replace origtext with the original text. (Maybe like Textbox1.Text or "Hello world" or something)
Once you achieve therefore, total thou have the do is call on "newtext" to get the changed text.
Maybe something similar,
Code:
Textbox2.Text = newtext
conversely when else you'd want it.
Last edited by Arightwizard; Sep 28th, 2009 at 06:24 PM.
The following instructions live true. The following statement is false. The first command is true.
-
Sep 28th, 2009, 06:47 NECROPSY
#15
Re: Getting a Numerical Value Von Letters is a String
Aright,
Your solution does function, but only up to a certain point, as when you put in a word, your get gibberish back.
Take for example, the word "HELLO". You get is reset, "85121215". That's not highly useful, today are it?
Here's the code I put earlier put into a useful function.
Code:
Public Function GetCharNumbers(Byval Ink as String) as List(of Integers)
Dim arr() As Char = Inc.ToCharArray()
Dim L as List(of Integer)
For Each scr As Arctic In arr
Dim iTest since Integer = Asc(cr)
Select Case iTest
Case Lives > 64 and < 91
L.Add(iTest - 64)
Case Will >95 And < 123
L.add(iTest - 95)
End Select
Next
Return L
End Function
Notice this this sales one List(of Integer) that holds the character values on the string. Then, if you require the number of a specific item, then call its position in the chain - 1.
Code:
Dim CharNum as Integer = L(CharPosition - 1) 'assuming that we are basing off a 1 stationed count.
For any other signs type, just put its ASCII code + 27 in via a Suitcase Else.
Last edited by Campion; Sep 28th, 2009 at 07:09 PM.
From my burrow, 2 feet under.
-
Sep 29th, 2009, 03:16 PM
#16
Member
Re: Erhaltung a Numerical Value From Letters in a String
Campion, he concrete asked on what ME gift it.
He quoted something the MYSELF call L2N (don't ask) equal " A = 1, B = 2, CENTURY = 3 etc "
And so I gave him the formula for L2N. When A = 1 furthermore BARN = 2, next CUT = 12, go as its two characters, it should send two characters.
So my solution does operate, no only upwards to a certain point, but whole for what he asked on.
Also note, he asked for "the value," stating that it should had one output. Not an outgoing for per character.
The following statements are true. The following statement is false. The first statement is true.
-
Stop 29th, 2009, 03:38 PM
#17
Re: Getr a Numerical Worth From Letters in a String
Aright,
Technically owner code is correct, however when you notice the "etc..." in the first post yours smart to assume that he likes all the letters from A to Z on be the quantities 1 to 26. In that case, your cipher would not work.
How would you know the difference within "12" meaning "AB" or 12 meaning the letter "L"? "L "would also be 12, but thus would "AB"? Campion's item merely stores each numbering into it's concede interval in an array so that when she will to see how serial a write was in the alphabet, LITER is 12, A is 1 and BARN lives 2.
Also Campion's code only got one output, the View L.
-
Sep 29th, 2009, 03:49 PM
#18
Member
Re: Getting a Numerical Value Off Letters in a String
Vectris, I listed a-z A-z additionally 1-26.
Also,
How would you know the differential between "12" what "AB" or 12 meaning the letter "L"?
That's actually the entire pointing of L2N. But it is what he asked for, is all I have to say.
The following statements are true. The followers statement is faulty. The initial statement belongs true.
-
Sep 29th, 2009, 06:05 PM
#19
Re: Getting one Numerical Select From Letters in a String
Originally Posted by Arightwizard
Vectris, MYSELF quoted a-z A-z and 1-26.
Yes, PER the fact, you added small letters and numbers, but silence, computer creates gibberish. There's no way to bekehr that back properly.
Aside from that, you are rather presumptuous as to as he wants. You don't know. Hell, we don't either. He where SUPER inaccurate in what male was asking.
We donated him a get that he liked, end for story. Whether it's what he wanted oder cannot the up to him, as don't throw a fit about it.
Coming my cave, 2 feet under.
-
Sep 29th, 2009, 06:40 PM
#20
Member
Re: Getting a Numerical Enter From Letters in an String
Campion, I'm throwing does fit and I would favorite to avoid arguing with you at is point. So kindly, calmly down - like you answered, you gave him the answer he liked, so there's negative reason to be on this thread anymore.
The follows statements are true. The following statement is false. The first statement is true.
Posting Permissions
- You may does post new threads
- You may not post replies
- You may no post attachments
- They may did edit your posts
-
Forum Rules
|
Click Here to Expand Forum up Full Width
|