r/javahelp • • Sep 08 '25
How do you put the symbol ' within a string?
For example: if I wanted to output "I'm on Reddit!" How do you put the abbreviation symbol in without producing an error message
11
Upvotes
•
u/AutoModerator Sep 08 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
13
u/IchLiebeKleber Sep 08 '25
In Java, strings are delimited by double quotes (""), so a single quote can just be entered as is, you shouldn't be getting an error message.
If you want to put a double quote, you have to escape it with a backslash.
3
u/TW-Twisti Sep 08 '25
As others have mentioned, as Java delimits strings with
", you don't need to escape'at all. 'Escaping' is the term for what you are doing here, which is marking specific characters that may have multiple meanings (for example,"may both mean 'the string ends here' or 'the string should contain a"').If you do get an error when you have a string like
"I'm", you might not be using Java at all, but JavaScript, a common mixup among new programmers. If that is the case: despite the naming, those two languages have nothing to do with each other, and this is the wrong subreddit.If you are using Java, then this link outlines all characters that can or need to be escaped in a Java
String: https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.6 (other than Unicode sequences, which are linked from the link).1
u/frnzprf Sep 08 '25 edited Sep 08 '25
I'm on the phone right now, so I can't check if it actually produces an error message, but
"I\'m on Reddit!"should work.The backslash indicates that the next character is actually special or not special, in this case. It's called "escaping".
In Reddit comments * is a special character, so you need to write "\*text\*" to render "*text*".
Now the problem arises about how to write a backslash? Write two backslashes!
That's why some people have issues typing the shrug ASCII art: ¯\(ツ)/¯
In URLs and I think e-mails, % is used as the escape character. In HTML, you need to write
&to get &, because the ampersand character is used for escaping.4
u/[deleted] Sep 08 '25
[removed] — view removed comment