Solved

Searching string and replacement

Posted on 2011-09-22
11
158 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
ID: 36580772
These are the top few lines too...

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

FlxGridInvoiceInfo(remove_minus, 14)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 40
ID: 36581021
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
ID: 36581027
Ok thanks. What is the common way to insert a value into a string?
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 36581254
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
ID: 36582141
>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
ID: 36582423
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
ID: 36585518
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
ID: 36585661
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
ID: 36585829
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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