The only way i believe the search would be legal was if it was pursuant to an inventory policy. Generally, the officer has no right to go through your wallet. It is likely an illegal search and seizure. However, i am not sure that it really results in any benefit to you. The bail fee was owed to the commissioner for appearing to bail you out. You would have had to arrange payment to him later. If you had previously promised the $40 or were holding someone else money you now simply must repay that person instead of the commissioner.
You should contact a lawyer about the DWI charge.
The search might have been in violation of your rights but what is the remedy? It does not should like anything would be suppress from this encounter at trial. You could be bring a lawsuit for violation of your rights but what are your damages?
My response to your question is a generic response and should not be construed as controlling to your case. I can not effectively advise about your case without knowing all the facts. Additionally, my response does not create an attorney-client relationship. You can contact my office to schedule an appointment if you would like to have me represent you.
The short answer is "no". The police can count your money, inventory your possessions, etc. in the interests of "protecting your property" while you are locked up. They cannot steal money from you or decide how you should spend your money for you.
Note this answer is general in nature and does not form or constitute an attorney-client relationship.
A roundup of the best tips and legal advice.