Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

how to clear a string that has multiple characters in it

OK I return a value from a COM port as the following:

<ack,123456CR<DR,1.56,123456CR

I am trying to key off two values that are duplicated in this string and if this is the case then clear the string and move on. If the string has the following sames characters "<" or "CR" which is Carriage return

How can I look at the string see that it carries two of the same values listed above and clear the string.
0
cmdolcet
Asked:
cmdolcet
  • 3
  • 2
2 Solutions
 
käµfm³d 👽Commented:
A regular expression could handle this.

e.g.

If System.Text.RegularExpressions.Regex.IsMatch(inputString, "([<\r]).*?\1") Then
    inputString = String.Empty
End If

Open in new window


The square brackets mean "either of these". The parentheses capture what is matched. The "\1" refers to this capture group. And the ".*?" means to match anything in between (including nothing). The overall effect is to match two of the same character (that you specified in the brackets) with anything in between.
0
 
cmdolcetAuthor Commented:
OK would it still be valid if the message was "<ack,123456CR" ?
because that is still acceptable
0
 
käµfm³d 👽Commented:
What do you mean by "valid". I took your original post to mean that you either would find two angle-brackets or two carriage returns, not one angle bracket and one carriage return. If there are not two (or more) of the same exact character in the string, then the string is left alone.
0
 
cmdolcetAuthor Commented:
OK I just had to make a slight change on your ismatch condition It was throwing an error saying it was missing a ")" so I added the following in "([\r].*?\1")  and then it did trigger an error however it is not picking up the invalid string.
0
 
käµfm³d 👽Commented:
There needs to be a closing paren to the right of the closing square bracket.

Here's just the string:

"([\r]).*?\1"

Open in new window

0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now