Mathematica has some very powerful string manipulation commands. Try the following:
In[1]:= modifyString[str_String] :=
ToLowerCase[StringReplace[str, Except[LetterCharacter] -> ""]]
In[2]:= modifyString["I am trying to write a function in Mathematica \
that takes a string and removes any spaces and punctuations and also \
turns capital letters into their lowercase equivalents, and keeps all \
lower case letters.
This is what I have so far, and it doesn't seem to work."]
Out[2]= "iamtryingtowriteafunctioninmathematicathattakesastringandremo\
vesanyspacesandpunctuationsandalsoturnscapitallettersintotheirlowercas\
eequivalentsandkeepsalllowercaselettersthisiswhatihavesofaranditdoesnt\
seemtowork"
If you want to allow digits as well then change LetterCharacter
above to WordCharacter
.
Note that in the above input and output the backslash characters are just a result of the copy and past from Mathematica into this forum. They are not part of the input or the output.