0

I have the following statement: binaryOutput = y % 2 + binaryOutput;

I want to the value of y % 2 to be added at the very beginning of binaryOutput. Is there a better way to do this operation than the way I did it? I feel like my method is a little redundant.

Edit: binaryOutput is a String object. y is an integer.

  • 1
    What type is `binaryOutput` and `y`? – xingbin Oct 04 '18 at 14:03
  • If you are building a string in a loop then using `StringBuilder` will be more efficient (but your code will not be shorter). – Jesper Oct 04 '18 at 14:04
  • Possible duplicate of [Insert a character in a string at a certain position](https://stackoverflow.com/questions/5884353/insert-a-character-in-a-string-at-a-certain-position) – Sir. Hedgehog Oct 04 '18 at 14:04
  • can you please provide an input output example and what is the type of each variable? – YCF_L Oct 04 '18 at 14:06
  • I would say there probably isn’t a better way to do it, especially because the operation is so basic. – 0xCursor Oct 04 '18 at 20:55

1 Answers1

0

USe the insert method of StringBuilder:

_sb.insert(0, "Hello ");
Jamie Snipes
  • 317
  • 2
  • 18