I was wondering if there is a way to extend the string class with a method called GenerateRandom() which generates a random string. Maybe this would look like  string.CreateRandom();   As I have read there is no way than creating an instance of a string and then calling the extension method. Maybe there is one and you know it.  Thanks for your help!
21166
I'm a fan of extension methods in C#, but haven't had any success adding an extension method to a static class, such as Console.  For example, if I want to add an extension to Console, called 'WriteBlueLine', so that I can go:  Console.WriteBlueLine("This text is blue");   I tried this by adding a local, public static method, with Console as a 'this' parameter... but no dice!      public static class Helpers {     public static void WriteBlueLine(this Console c, string text)     {         Console.ForegroundColor = ConsoleColor.Blue;         Console.WriteLine(text);         Console.ResetColor();     } }   This didn't add a 'WriteBlueLine' method to Console... am I doing it wrong? Or asking for the impossible?
92031
Whats this syntax useful for :       function(String... args)   Is this same as writing       function(String[] args)    with difference only while invoking this method or is there any other feature involved with it ?
5812
Why does the  exist in modern languages such as C# and Java? Is it purely a self documenting code feature, or does it serve any actual purpose?  For instance the following example:  Class1 obj = new Class1();  Class1 foo() {     return new Class1(); }   Is as easy to read as the more Pythonesque way of writing it:  Class1 obj = Class1();  Class1 foo() {     return Class1(); }   EDIT:  hit the nail on the head with the clarification of the question: Why did they choose this syntax?
13955
Unless I am missing an obvious built-in method, what is the quickest way to get the nth occurrence of a string within a string?  I realize that I could loop the  method by updating its start index on each iteration of the loop. But doing it this way seems wasteful to me.
14769
I need to concatenate two String arrays in Java.  void f(String[] first, String[] second) {     String[] both = ??? }   What is the easiest way to do this?
23463
I need a quick algorithm to select 5 random elements from a generic list. For example, I'd like to get 5 random elements from a List&lt;string&gt;.
118610
I remember seeing a while ago that there is some method in maybe the Reflection namespace that would recursively run ToString() on all of an object's properties and format it nicely for display.    Yes, I know everything I could want will be accessible through the debugger, but I'm wondering if anyone knows that command?
114309
How to calculate the length (in pixels) of a string in Java?  Preferable without using Swing.  EDIT: I would like to draw the string using the drawString() in Java2D  and use the length for word wrapping.
17166
I have a list of integers, List&lt;Integer&gt; and I'd like to convert all the integer objects into Strings, thus finishing up with a new List&lt;String&gt;.  Naturally, I could create a new List&lt;String&gt; and loop through the list calling String.valueOf() for each integer, but I was wondering if there was a better (read: more automatic) way of doing it?
97272
What is the most efficient way to get a random output with my commands? In Java there are tons of ways to do this, but a lot of them don’t seem to work for Bedrock Edition.
75336
Someone told me it's more efficient to use StringBuffer to concatenate strings in Java than to use the + operator for Strings. What happens under the hood when you do that? What does StringBuffer do differently?
75990
We have to build Strings all the time for log output and so on. Over the JDK versions we have learned when to use StringBuffer (many appends, thread safe) and StringBuilder (many appends, non-thread-safe).  What's the advice on using String.format()? Is it efficient, or are we forced to stick with concatenation for one-liners where performance is important?  e.g. ugly old style,  String s = "What do you get if you multiply " + varSix + " by " + varNine + "?";   vs. tidy new style (String.format, which is possibly slower),  String s = String.format("What do you get if you multiply %d by %d?", varSix, varNine);   Note: my specific use case is the hundreds of 'one-liner' log strings throughout my code. They don't involve a loop, so StringBuilder is too heavyweight. I'm interested in String.format() specifically.
16650
Can anyone think of any situation to use multiple inheritance? Every case I can think of can be solved by the method operator   AnotherClass() { return this-&gt;something.anotherClass; }
61737
I would like to use Class.newInstance() but the class I am instantiating does not have a nullary constructor. Therefore I need to be able to pass in constructor arguments. Is there a way to do this?
113031
I have a dll that must be useable from C etc, so I cant use string objects etc as a normal would, but I'm not sure on how to do this safely..  const char *GetString() {     std::stringstream ss;     ss &lt;&lt; "The random number is: " &lt;&lt; rand();     return ss.str().c_str(); }   could the c string be destroyed when ss falls off the stack? I'm  assuming so...  Another option may be to create a new string on the heap, but what is going to deallocate that?  const char *GetString() {     std::stringstream ss;     ss &lt;&lt; "The random number is: " &lt;&lt; rand();     char *out = new char[ss.str().size()];     strcpy(ss.str().c_str(), out);     return out;//is out ever deleted? }   The same goes for pointers to other things as well as strings.
4153
Is there some easy way to pad Strings in Java?  Seems like something that should be in some StringUtil-like API, but I can't find anything that does this.
1992
While working in a Java app, I recently needed to assemble a comma-delimited list of values to pass to another web service without knowing how many elements there would be in advance. The best I could come up with off the top of my head was something like this:  public String appendWithDelimiter( String original, String addition, String delimiter ) {     if ( original.equals( "" ) ) {         return addition;     } else {         return original + delimiter + addition;     } }  String parameterString = ""; if ( condition ) parameterString = appendWithDelimiter( parameterString, "elementName", "," ); if ( anotherCondition ) parameterString = appendWithDelimiter( parameterString, "anotherElementName", "," );   I realize this isn't particularly efficient, since there are strings being created all over the place, but I was going for clarity more than optimization.  In Ruby, I can do something like this instead, which feels much more elegant:  parameterArray = []; parameterArray &lt;&lt; "elementName" if condition; parameterArray &lt;&lt; "anotherElementName" if anotherCondition; parameterString = parameterArray.join(",");   But since Java lacks a join command, I couldn't figure out anything equivalent.  So, what's the best way to do this in Java?