Link to home
Start Free TrialLog in
Avatar of deanlee17
deanlee17

asked on

Searching string and replacement

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

Avatar of deanlee17
deanlee17

ASKER

These are the top few lines too...

Dim remove_minus As Integer
For remove_minus = 1 To FlxGridInvoiceInfo.Rows.Count - 1
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
The string will be the value in the flexgrid as it looks through...

FlxGridInvoiceInfo(remove_minus, 14)
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
Ok thanks. What is the common way to insert a value into a string?
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Nasir Razzaq
>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
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.
Ok just trying both solutions to see what i get,

Code cruiser yours errored with....

MissingMemberException
Public member 'StartsWith' on type 'Single' not found.
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

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