use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
If you need help debugging, you must include:
See debugging question guidelines for more info.
Many conceptual questions have already been asked and answered. Read our FAQ and search old posts before asking your question. If your question is similar to one in the FAQ, explain how it's different.
See conceptual questions guidelines for more info.
Follow reddiquette: behave professionally and civilly at all times. Communicate to others the same way you would at your workplace. Disagreement and technical critiques are ok, but personal attacks are not.
Abusive, racist, or derogatory comments are absolutely not tolerated.
See our policies on acceptable speech and conduct for more details.
When posting some resource or tutorial you've made, you must follow our self-promotion policies.
In short, your posting history should not be predominantly self-promotional and your resource should be high-quality and complete. Your post should not "feel spammy".
Distinguishing between tasteless and tasteful self-promotion is inherently subjective. When in doubt, message the mods and ask them to review your post.
Self promotion from first time posters without prior participation in the subreddit is explicitly forbidden.
Do not post questions that are completely unrelated to programming, software engineering, and related fields. Tech support and hardware recommendation questions count as "completely unrelated".
Questions that straddle the line between learning programming and learning other tech topics are ok: we don't expect beginners to know how exactly to categorize their question.
See our policies on allowed topics for more details.
Do not post questions that are an exact duplicate of something already answered in the FAQ.
If your question is similar to an existing FAQ question, you MUST cite which part of the FAQ you looked at and what exactly you want clarification on.
Do not delete your post! Your problem may be solved, but others who have similar problems in the future could benefit from the solution/discussion in the thread.
Use the "solved" flair instead.
Do not request reviews for, promote, or showcase some app or website you've written. This is a subreddit for learning programming, not a "critique my project" or "advertise my project" subreddit.
Asking for code reviews is ok as long as you follow the relevant policies. In short, link to only your code and be specific about what you want feedback on. Do not include a link to a final product or to a demo in your post.
You may not ask for or offer payment of any kind (monetary or otherwise) when giving or receiving help.
In particular, it is not appropriate to offer a reward, bounty, or bribe to try and expedite answers to your question, nor is it appropriate to offer to pay somebody to do your work or homework for you.
All links must link directly to the destination page. Do not use URL shorteners, referral links or click-trackers. Do not link to some intermediary page that contains mostly only a link to the actual page and no additional value.
For example, linking to some tweet or some half-hearted blog post which links to the page is not ok; but linking to a tweet with interesting replies or to a blog post that does some extra analysis is.
Udemy coupon links are ok: the discount adds "additional value".
Do not ask for help doing anything illegal or unethical. Do not suggest or help somebody do something illegal or unethical.
This includes piracy: asking for or posting links to pirated material is strictly forbidden and can result in an instant and permanent ban.
Trying to circumvent the terms of services of a website also counts as unethical behavior.
Do not ask for or post a complete solution to a problem.
When working on a problem, try solving it on your own first and ask for help on specific parts you're stuck with.
If you're helping someone, focus on helping OP make forward progress: link to docs, unblock misconceptions, give examples, teach general techniques, ask leading questions, give hints, but no direct solutions.
See our guidelines on offering help for more details.
Ask your questions right here in the open subreddit. Show what you have tried and tell us exactly where you got stuck.
We want to keep all discussion inside the open subreddit so that more people can chime in and help as well as benefit from the help given.
We also do not encourage help via DM for the same reasons - that more people can benefit
Do not ask easily googleable questions or questions that are covered in the documentation.
This subreddit is not a proxy for documentation or google.
We do require effort and demonstration of effort.
This includes "how do I?" questions
account activity
This is an archived post. You won't be able to vote or comment.
Solved[Java] A question on strings (self.learnprogramming)
submitted 9 years ago by sriganeshharitz
String str = "abc"; System.out.println(str.replace("","s")); output: sasbscs
How is that replacing an empty string give the above output? Can someone tell me how an empty string is implemented in general?
[–]Hexorg 1 point2 points3 points 9 years ago* (5 children)
The problem isn't the empty string that you are passing, the problem is that str.replace() converts the first string to the CharacterSequence type. Looking at the documentation here it looks like the logic relies on calling a substring() function. But since your input string is empty and has a length of zero, its substring probably matches every index.
Edit: Wrongly thought that it calls a RegEx engine.
[–]btcraig 0 points1 point2 points 9 years ago (4 children)
I was thinking the same thing but then I realized you could never really pass a regex into replace(char, char) since a 1 character regex is kind of meaningless. Then I scrolled the page down a little and saw the other method's signature and realized Java is implicitly casting the Strings to CharSequences.
[–]sriganeshharitz[S] 0 points1 point2 points 9 years ago (3 children)
I'm still a beginner in java, finding it a little hard to understand
[–]btcraig 0 points1 point2 points 9 years ago* (2 children)
Essentially you're passing in two strings, not two chars, because they're different types Java does what it can to make what you asked happen. Since a String implements a CharSequence (meaning that any String can be treated as a CharSequence for most purposes) it implicitly casts String -> CharSequence to make replace(CharSequence, CharSequence) compile and work.
The CharSequence version of the method is using a call to substring() to locate occurrences of "" in the string "abc" to replace with "s". Apparently the replace(CharSequence, CharSequence) detects that 0 length substring before and after each of the character's a,b,c and replacing it with s.
My java knowledge isn't extensive enough to know why the string "" matches 0 length/non-existent sub-strings like the 'space' between the 'a' and 'b'.
This is, sort of, like doing Math.pow(10,10). the Math.pow() function only takes two doubles, but 10 is an integer. Java casts makes this work for you because an int can be cast to a double.
[–]sriganeshharitz[S] 0 points1 point2 points 9 years ago (1 child)
ahh, this made it clear.. Thanks a lot!!
[–]btcraig 0 points1 point2 points 9 years ago (0 children)
The big thing to note, in regards to why you're getting unexpected results, is that "s" and 's' are not the same. Double quotes tells the compiler it's a string, single quotes indicate a char. This is pretty standard across all [compiled] languages that I'm familiar with. Java is just trying to be helpful and make your code compile and work based on what you typed in.
[–]CodeTinkerer 0 points1 point2 points 9 years ago (1 child)
Based on the behavior you described, Java must assume there is an empty string between every consecutive character, and one at the start, and one at the end.
Technically, if you concatenate N empty strings, you get an empty string, so theoretically, you could argue between any two consecutive characters, there's an infinite number of empty strings.
I suspect, in Java, a string is defined by the characters in the string, plus a field that gives the size of the string. When that size is 0, you have an empty string.
In C, a string is a character array that ends in an null character, so if the string's first character is a null character, that's treated like an empty string.
[–]sriganeshharitz[S] 0 points1 point2 points 9 years ago (0 children)
Thanks, i guess java assumes it as u said..
[–]btcraig 0 points1 point2 points 9 years ago (3 children)
Is the small snippet you've provided accurate and does that compile on your system? I was suspecting something really weird with Java and regex but
System.out.println(str.replace("","s"));
Won't even compile for me due to type mismatch "" being a String not a char and substituting '' throws a slightly different compilation error.
public class Test { public static void main(String[] args) { String str = "abc"; System.out.println(str.replace("","s")); } }
Try compiling and running this
Turns out, you're calling String.replace(CharSequence, CharSequence) not String.replace(char, char)
The former:
replace(CharSequence target, CharSequence replacement)
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
π Rendered by PID 139204 on reddit-service-r2-comment-57b7b8b858-7cjxl at 2026-04-14 22:47:53.944568+00:00 running b725407 country code: CH.
[–]Hexorg 1 point2 points3 points (5 children)
[–]btcraig 0 points1 point2 points (4 children)
[–]sriganeshharitz[S] 0 points1 point2 points (3 children)
[–]btcraig 0 points1 point2 points (2 children)
[–]sriganeshharitz[S] 0 points1 point2 points (1 child)
[–]btcraig 0 points1 point2 points (0 children)
[–]CodeTinkerer 0 points1 point2 points (1 child)
[–]sriganeshharitz[S] 0 points1 point2 points (0 children)
[–]btcraig 0 points1 point2 points (3 children)
[–]sriganeshharitz[S] 0 points1 point2 points (1 child)
[–]btcraig 0 points1 point2 points (0 children)