0

I am just wondering if it is possible to check a string for specific characters. For example if my string is String mystr = "test"; and I wanted to check for the letter t at the beginning and if it is NOT there do something how would I go about this?

I have read about string.startsWith(string2); however it returns true or false, I want to be able to do "XYZ" if "ABC" isn't satisfied, and it would seem that string.contains isn't a thing.

To paraphrase, I am trying to create an if statement that will evaluate if a string starts with a certain character(s) and will do something if this condition is NOT met.

asked Sep 9, 2016 at 19:34
4
  • In C and C++, the ! operator means "not". E.g. if (!(the condition)) { some_action(); } means "do some action if the condition is not satisfied". Commented Sep 9, 2016 at 19:47
  • Yes! I thought that I might be able to adapt the string.startsWith, however it flagged saying expected unqualified id before '!' token Commented Sep 9, 2016 at 23:04
  • What did you write to get that error? If you don't show us your code, we won't be able to help. Commented Sep 10, 2016 at 7:40
  • I messed up and put the ! in the wrong place, sorry to have not caught that. Commented Sep 10, 2016 at 17:01

1 Answer 1

0

Have you tried the following?

if(!actualString.startsWith(expectedString)) {
 // do whatever you have to
}

BTW, that is what actually suggested in @EdgarBonet's comment.

answered Sep 10, 2016 at 12:01
1
  • That did it, I had the ! in the wrong place. Commented Sep 10, 2016 at 17:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.