![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | String to int conversion Hi, I have a string str which can take any odd values from 01 to 99. I need to convert that to the next positive number. Therefore 03 becomes 04 25 becomes 26 01 becomes 02 With the code I have below I get 03 to be 4. So I loose the 0 which is crucial to me. Any suggestions as to how to solve this issue. str="03" str=Cint(str) + 1 wscript.echo str Thanks, SA |
My System Specs![]() |
| | #2 (permalink) |
| | Re: String to int conversion <karsagarwal@xxxxxx> wrote in message news:24d37819-2dae-426c-9d57-f11bf1f06ada@xxxxxx Quote: > Hi, > > I have a string str which can take any odd values from 01 to 99. I > need to convert that to the next positive number. > > Therefore 03 becomes 04 > 25 becomes 26 > 01 becomes 02 > > With the code I have below I get 03 to be 4. So I loose the 0 which is > crucial to me. Any suggestions as to how to solve this issue. > > > str="03" > str=Cint(str) + 1 > wscript.echo str > (assuming always 2 digits) would be: str = "03" str = Right("0" & CStr(CInt(str) + 1), 2) Wscript.Echo str -- Richard Mueller MVP Directory Services Hilltop Lab - http://www.rlmueller.net -- |
My System Specs![]() |
| | #3 (permalink) |
| | Re: String to int conversion In microsoft.public.scripting.vbscript message <24d37819-2dae-426c- 9d57-f11bf1f06ada@xxxxxx>, Thu, 11 Sep 2008 18:10:37, "karsagarwal@xxxxxx" <karsagarwal@xxxxxx> posted: Quote: > >str="03" >str=Cint(str) + 1 >wscript.echo str wscript.echo Right(str+101, 2) ASIDE - it appears that one cannot (one can in JavaScript) apply unary plus and convert String to Number - document.write +"03" gives '03'. But one can apply unary minus and convert - document.write -"03" gives '-3' and one can apply it twice - document.write --"03" gives '3' . -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links. Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036) Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036) |
My System Specs![]() |
| | #4 (permalink) |
| | Re: String to int conversion "Dr J R Stockton" wrote: Quote: > str="03" > wscript.echo Right(str+101, 2) In so many other uses of string+number, I have seen that the result is string concatenation. Why this simple usage works is a mystery to me. Oh, I understand what happens, under the covers. VBScript attempts to coerce the two values being "added" to the same data type, using VariantChangeTypeEx. But it has been my experience *many* times in the past that the preferred conversion is integer to string (so that then the + operator causes concatenation). Wish I had a handy example; if I find one I'll repost. But anyway, clearly in a simple situation such as this (one string, one integer) it works exactly as you would want it to. I wonder if the precendence (string vs. number) has changed between VBS 3 and VBS 5? Because when I was working in the source code of VBS 3 (about 1999), I pretty clearly remember it the other way around. Oh, well. Maybe not. Maybe senility has set in. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| conversion from .net String to mfc CString | .NET General | |||
| problems with $var | select-string -pattern $string -q | PowerShell | |||
| bug in string<->datetime conversion? | PowerShell | |||
| How export-csv deals with string versus string[] | PowerShell | |||
| String PRODUCT_NAME was not found in string table | Vista General | |||