Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / July 2007

Tip: Looking for answers? Try searching our database.

string work

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jdrott1 - 30 Jul 2007 21:43 GMT
i'm trying to change a number inside a string.  (i.e. 5.39)  if the
number in position 2  is < = 4 then change it to 4  if its > = 5
change it to a 9.

how would i do this in vb 2005?

thanks.
Cor Ligthert[MVP] - 30 Jul 2007 23:15 GMT
jdrott,

You can use the If and the Mid command for this.

As you are familiar with VB6 it is the same as that.

Cor

> i'm trying to change a number inside a string.  (i.e. 5.39)  if the
> number in position 2  is < = 4 then change it to 4  if its > = 5
[quoted text clipped - 3 lines]
>
> thanks.
Jack Jackson - 31 Jul 2007 00:33 GMT
>i'm trying to change a number inside a string.  (i.e. 5.39)  if the
>number in position 2  is < = 4 then change it to 4  if its > = 5
>change it to a 9.
>
>how would i do this in vb 2005?

I doubt you really want to do what you say.  I'm not sure which
character you think is in position 2, so I assumed it is the '3'.  You
probably don't want to assume that the decimal point is the second
character, use _str.IndexOf("."c) to find out where it is.  You
probably also don't want the second Substring call in the last line
which keeps the digits beyond the one that is changed.

Dim _chr As String
Dim _index As Integer = 2
Dim _str As String = "5.39"

If _str.Substring(_index, 1).ToInt32 <= 4
   _chr = "4"
Else
  _chr = "9"
End If
_str = _str.Substring(0, _index) + _chr + _str.SubString(_index + 1)
Göran Andersson - 31 Jul 2007 10:45 GMT
> i'm trying to change a number inside a string.  (i.e. 5.39)  if the
> number in position 2  is < = 4 then change it to 4  if its > = 5
> change it to a 9.

Why do you have the number in a string? As what you want is a numeric
operation, convert it to a number.

Dim price as Double
price = Double.Parse(theString)
price += 0.01
price *= 2
price = Math.Floor(price)
price /= 2
price -= 0.01

Use price.ToString("0.00") to create a string in the same format as the
original.

Signature

Göran Andersson
_____
http://www.guffa.com

Göran Andersson - 31 Jul 2007 11:47 GMT
>> i'm trying to change a number inside a string.  (i.e. 5.39)  if the
>> number in position 2  is < = 4 then change it to 4  if its > = 5
[quoted text clipped - 8 lines]
> price *= 2
> price = Math.Floor(price)

That should of course be Math.Ceiling.

> price /= 2
> price -= 0.01
>
> Use price.ToString("0.00") to create a string in the same format as the
> original.

Signature

Göran Andersson
_____
http://www.guffa.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.