Solved

how to clear a string that has multiple characters in it

Posted on 2013-11-25
5
217 Views
Last Modified: 2013-11-25
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
Comment
Question by:cmdolcet
  • 3
  • 2
5 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 305 total points
ID: 39675705
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
 

Author Comment

by:cmdolcet
ID: 39675711
OK would it still be valid if the message was "<ack,123456CR" ?
because that is still acceptable
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39675718
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
 

Author Comment

by:cmdolcet
ID: 39675770
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
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 305 total points
ID: 39675785
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
vb.net help 4 34
Database Connections Not being returned to Connection Pool 7 34
vb.net cycle through results of a sql query and capture variables 3 18
Code enhancement 4 20
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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