Solved

Searching string and replacement

Posted on 2011-09-22
11
155 Views
Last Modified: 2012-05-12
Hi experts,

Trying to check the first char of a string. If its aminus I want it stripped out, if one doesnt exist then I want it added.

Thanks.
If IsDBNull(FlxGridInvoiceInfo(remove_minus, 14)) = False And (FlxGridInvoiceInfo(remove_minus, 15) = "Stock Adj") Then

                                       if first char is a minus strip it out
                  

                    Else
place a minus before the value

                    End If


                End If

Open in new window

0
Comment
Question by:deanlee17
  • 6
  • 4
11 Comments
 

Author Comment

by:deanlee17
Comment Utility
These are the top few lines too...

Dim remove_minus As Integer
For remove_minus = 1 To FlxGridInvoiceInfo.Rows.Count - 1
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
I do not see the string in you code, but here is a way to do it, given a string called s.

s = (-CInt(s)).ToString
0
 

Author Comment

by:deanlee17
Comment Utility
The string will be the value in the flexgrid as it looks through...

FlxGridInvoiceInfo(remove_minus, 14)
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
And how do you reference the value in the flexgrid? Sorry, flexgrid is not a standard control and I do not use it, so I cannot give you the exact syntax.

Maybe something similar to this:

grid.Cells(remove_minus,3).Value = (-CInt(grid.Cells(remove_minus ,3).Value)).ToString
0
 

Author Comment

by:deanlee17
Comment Utility
Ok thanks. What is the common way to insert a value into a string?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
If the value is already a String, simply assign it directly:

Dim s As String
s=value

If not, the following does the job for most types of values:

Dim s As String
s=value.ToString

If not, then the value might have another method to do that:

Dim s As String
s=value.<Other Method>
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
>If its aminus I want it stripped out, if one doesnt exist then I want it added.

Generally speaking, you can use string functions

Dim str as String = "-123"

If str.StartsWith("-") Then
   Str.TrimStart("-")
Else
   Str = "-" & str
End If
0
 
LVL 40

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
Manipulating strings as suggested by CodeCruiser can lead to problems in some situations.

The format of a negative number, including the position of the -, can be changed in the configuration panel. Assuming - at the beginning can lead to problems. You could well end up with -123- instead of removing the -.

This is why, in my solutions, I convert to an Interger and then back to a String. The conversion will take care of any format set in the Control Panel.
0
 

Author Comment

by:deanlee17
Comment Utility
Ok just trying both solutions to see what i get,

Code cruiser yours errored with....

MissingMemberException
Public member 'StartsWith' on type 'Single' not found.
0
 

Author Comment

by:deanlee17
Comment Utility
So ive now tried..

Didnt error but when the msgbox displayed the value it had not trimmed off any minuses

Thans
If IsDBNull(FlxGridInvoiceInfo(remove_minus, 14)) = False And (FlxGridInvoiceInfo(remove_minus, 15) = "Stock Adj") Then

                  
                    If FlxGridInvoiceInfo(remove_minus, 14).ToString.StartsWith("-") Then
                        FlxGridInvoiceInfo(remove_minus, 14).ToString.TrimStart("-")

                        MsgBox(FlxGridInvoiceInfo(remove_minus, 14))
                       
                    Else

                        MsgBox("Something")



                    End If

Open in new window

0
 

Author Comment

by:deanlee17
Comment Utility
Nailed it, see attached....
If IsDBNull(FlxGridInvoiceInfo(remove_minus, 14)) = False And (FlxGridInvoiceInfo(remove_minus, 15) = "Stock Adj") Then

                    Dim Thestring As String = FlxGridInvoiceInfo(remove_minus, 14)
                    Dim addminus As String = "-"

                    If Thestring.StartsWith("-") Then

                        Thestring = Replace(Thestring, "-", "")

                        FlxGridInvoiceInfo(remove_minus, 14) = Thestring

                 

                    Else

                        FlxGridInvoiceInfo(remove_minus, 14) = addminus & Thestring


                    End If


                End If

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now