Sponsored Links
Sponsored Link

sponsored links

Collapse

Announcement

Collapse
No announcement yet.

Check if a string has certain characters

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • RDKells
    started a topic Check if a string has certain characters

    Check if a string has certain characters

    Hello,

    I have a string that has text in it like 'Description' and I want to check it doesn't contain the following 3 characters; . ' "

    After googling and testing myself, as far I can see the only options I have are;

    1. Use xlate (which would involve putting every allowed value into a string to compare... not feasible)
    2. Use SCAN on each character

    Surely there's a way of doing this on 1 line?

    Cheers,
    Ryan

  • RDKells
    commented on 's reply
    You could argue that - however you could probably do something similar with SQL, so technically it's not a specific function to check a string for a list of characters it's more enhanced string manipulation functionality that allows you to achieve what you want, after a fair amount of lines! Lol.

  • RDKells
    commented on 's reply
    Probably could've been worded better but don't worry about it now, the others understood it and it's been answered.

    Cheers,
    Ryan

  • Vectorspace
    commented on 's reply
    If it's a comma (,) rather than a full stop (.), then the code for my two methods would be:

    Code:
    // Scan xlate
    If %scan(',':%xlate('''"':',,':str)) > 0;
      // Disallowed character found
    EndIf;
    
    //regex:
    found = *off;
    exec sql set :found =
      case when REGEXP_LIKE(trim(:str),'[,''"]')
           then '1' else '0' end;
    If found 
      // Disallowed character found
    EndIf;
    You could probably argue that REGEXP_LIKE is the function for this problem (and more complex variants of it), but it's a recent enough addition that not all systems have the OS version or PTF yet.

  • CRinger400
    replied
    I'm confused. The title basically says "contains certain characters" then the 1st sentence says "DOESN'T contain certain characters". Which is it? Then the example says 3 characters but ; . ' " are 4 characters.

    Leave a comment:


  • RDKells
    commented on 's reply
    Sorry, not sure why it's a . I meant a ,!

    scan xlate - I see your logic there, that makes sense.

    Looks like I'll just keep the 3 scans then, I find it odd that there isn't a function for this though but I guess it's never really been required?

  • RDKells
    commented on 's reply
    Sorry you're right - I recall now, it kept returning 1 when I was expecting 0 because it found the first character.

  • Brian Rusch
    replied
    Originally posted by RDKells View Post
    Thanks for the replies.
    I tried %Check initially but after testing I realised it's looking for an exact match...
    To clarify, the %Check function does not look for an exact match. It looks for any characters that are not in the list of characters given, which is the opposite of what you want.

    Leave a comment:


  • Vectorspace
    replied
    But I thought you said that '.' was one of the characters you wanted to check for?
    and I want to check it doesn't contain the following 3 characters; . ' "
    That's the only reason I said convert to '.'

    You are right, the fundamentals of the idea is if you have multiple characters, lets call them A, B, and C, then use xlate to convert all instances of B and C into A. Then all you have to do is scan for A



    The reason I think this method would be less efficient than three separate %scan()s is it seems to me that the three scans each only have to find the first instance of each character, whereas the xlate method has to find and replace every instance of 2 of them, which seems to me to be more work

    3 scans:
    • Find first instance of char 1
    • return index
    • Find first instance of char 2
    • return index
    • Find first instance of char 3
    • return index
    scan xlate:
    • Find every instance of char 2
    • Replace all with char 1
    • Find every instance of char 3
    • Replace all with char 1
    • Find first instance of char 1
    • return index

    Leave a comment:


  • RDKells
    commented on 's reply
    3rd time's a charm?

    Code:
    if %scan('"':%xlate('''',':'"':str)) > 0; 
      // Disallowed character found 
    endif;

  • RDKells
    commented on 's reply
    Forgot code tags and can't edit;
    Code:
    if %scan('.':%xlate('''"':'..':str)) > 0;
      // Disallowed character found
    endif;

  • RDKells
    replied
    Good idea, as always.

    The only issue with that is if they have entered a '.' in the string it would fail - I suppose the fix would be to convert 2 of the 3, to the 3rd and scan for that, i.e convert , and ' to " then scan for " i.e;

    if %scan('"':%xlate('''',':'"':str)) > 0; // Disallowed character found endif; But you say this is less efficient? How come?

    Leave a comment:


  • Vectorspace
    replied
    I think REGEXP_LIKE() was introduced in i7.1 in a PTF


    I have had another idea, this could be a single line

    Use %xlate to replace every disallowed character with the same character, then you can scan for that single character.
    I.e. your disallowed characters are . ' " so replace ' and " with . and then scan for .

    Code:
    if %scan('.':%xlate('''"':'..':str)) > 0;
      // Disallowed character found
    endif;
    A separate %scan for each character would still be the most efficient in terms of processing, but this is the one line solution you were after.

    Leave a comment:


  • RDKells
    replied
    Thanks for the replies.
    I tried %Check initially but after testing I realised it's looking for an exact match.
    SCANRPL is a good idea, but doesn't it work the same as scan??? I.e. it looks for an exact match, so I'd need 3 lines anyway?


    I use regex a lot, yeah - I did see a post on here a while ago, where someone suggested SQL & Regex but when I tried it out to see if it would work, it couldn't find the function so I guess we don't have the PTF. I've checked our new development machine (V7R3) and it does have it but the client machines don't, so I guess if I did this and sent it across it would fail because the function doesn't exist?

    I'll save that post though because I'm sure it will come in handy in the future.

    Thanks again,
    Ryan

    Leave a comment:


  • wegrace
    replied
    Post Deleted.
    Last edited by wegrace; June 11th, 2018, 09:12 AM.

    Leave a comment:

sponsored links

Collapse

Working...
X