Solved

how to clear a string that has multiple characters in it

Posted on 2013-11-25
5
211 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

13 Experts available now in Live!

Get 1:1 Help Now