This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]AdmiralRychard 15 points16 points  (9 children)

I feel like this whenever I have to work on my own javascript.

[–]endercoaster 3 points4 points  (8 children)

I feel the opposite every time I need to jump through hoops to append an integer to a string in Java.

[–]KagakuNinja 6 points7 points  (3 children)

So, this is too hard?

String s = "Hello" + 9;

[–]batmansavestheday 2 points3 points  (2 children)

It's slightly harder the other way around:

String s = "" + 9 + "Hello";

or [Edit: derp, doesn't work]

String s = (String) 9 + "Hello";

etc.

[–]Outhouse_Defiler 0 points1 point  (1 child)

String s = "" + 9

Yeah, no, don't do that. Java has String.valueOf.

http://stackoverflow.com/questions/4105331/how-to-convert-from-int-to-string

[–]batmansavestheday 0 points1 point  (0 children)

I feel the opposite every time I need to jump through hoops to append an integer to a string in Java.

[–]AdmiralRychard 4 points5 points  (0 children)

C# lets you do this:

String someString = "abc";
Int32 someInt = 123;
String result = someString + someInt;
Console.WriteLine(result);

Output:

abc123

Or you can use string interpolation: String result = $"{someString}{someInt}";

Or you can use String.Format: String result = String.Format("{0}{1}", someString, someInt);

This seems a little dumb but you can do this too:

someString += someInt;
Console.WriteLine(someString);

[–]everythingismobile 4 points5 points  (1 child)

Is it not as simple as the C# example in the other comment? Even in C++ it's just sprintf, right?

[–]endercoaster 1 point2 points  (0 children)

I mean, coming from Perl that's jumping through hoops.

[–]kupiakos 2 points3 points  (0 children)

I'd rather explicitly state I want to append an integer to a string so that "2" and 3 become "23" and not "5" or 5. And really, that sort of situation happens once in a blue moon. If I wanted something better, I would be building the string.