(Sorry for the weird title, but I can't figure out what actually the problem is)  The following code should get a String from the command line first (which works), then the input is being splitted (works perfectly, too; I checked by printing both Strings before the if/else as you can see in the part I commented out again) and then it should check what the first part of the splitted String is. And for example if it equals "tweet" it should procedure with the Tweet method.  But somehow it doesn't get that right. It always executes the else statement...  Scanner sc = new Scanner(System.in); System.out.print("&gt; "); String input = sc.nextLine();  String[] splitString = input.split(" "); if(splitString.length != 2){ throw new IllegalArgumentException(); } String command = splitString[0]; String value = splitString[1];  /*System.out.print(command); System.out.print(value);*/ if(command == "tweet") { Tweet(value); } else if(command == "help") { ShowHelp(); } else { System.out.println("Command "+command+" not found."); }   I tried entering "tweet asdf", but it returns  &gt; tweet asdf Command tweet not found.   What did I do wrong? I'm confused D:
93
I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.  Is == bad? When should it and should it not be used? What's the difference?
27528
I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.    Is there a function that can do this easily?  For example:  $string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing."; $string2 = 100charfunction($string1); print $string2   To get:  I am looking for a way to pull the first 100 characters from a string vari
62905
Look at this if block:  #!/bin/bash  str="m.m" if [[ "${str}" =~ "m\.m" ]]; then     echo "matched" else     echo "not matched"     exit 1 fi  exit 0   This should print "matched", but it doesn't. Where am I going wrong?
98204
During my work with databases I noticed that I write query strings and in this strings I have to put several restrictions in the where-clause from a list/array/collection. Should look like this:  select * from customer  where customer.id in (34, 26, ..., 2);   You can simplify this by reducing this to the question that you have collection of strings and want to create a comma-separated list of this strings in just one string.  My approach I have used so far is something like that:  String result = ""; boolean first = true; for(String string : collectionOfStrings) {     if(first) {         result+=string;         first=false;     } else {         result+=","+string;     } }   But this is as you can see very ugly. You cannot see what happens there on the first look, especially when the constructed strings (like every SQL query) is getting complicated.  What is your (more) elegant way?
11923
I'm trying to split a string up into words and punctuation, adding the punctuation to the list produced by the split.  For instance:  &gt;&gt;&gt; c = "help, me" &gt;&gt;&gt; print c.split() ['help,', 'me']   What I really want the list to look like is:  ['help', ',', 'me']   So, I want the string split at whitespace with the punctuation split from the words.  I've tried to parse the string first and then run the split:  &gt;&gt;&gt; for character in c: ...     if character in ".,;!?": ...             outputCharacter = " %s" % character ...     else: ...             outputCharacter = character ...     separatedPunctuation += outputCharacter &gt;&gt;&gt; print separatedPunctuation help , me &gt;&gt;&gt; print separatedPunctuation.split() ['help', ',', 'me']   This produces the result I want, but is painfully slow on large files.  Is there a way to do this more efficiently?
70818
My question is on return values produced by this code:  if [ -n ]; then echo "true"; else echo "false"; fi   This prints true.  Its complementary test using [ -z ] also prints true:  if [ -z ]; then echo "true"; else  echo "false"; fi   In the above code, why does the [ -n ] test assume the string value that is not passed at all, as not null?  The code below prints false. This is expected since the passed string value is null and of zero length.  if [ -n "" ]; then echo "true"; else  echo "false"; fi
62033
I have a question about comparing a string with the empty string in Java. Is there a difference, if I compare a string with the empty string with == or equals? For example:  String s1 = "hi";  if (s1 == "")   or   if (s1.equals(""))    I know that one should compare strings (and objects in general) with equals, and not ==, but I am wondering whether it matters for the empty string.
108624
I have a string in the next format  id;some text here with possible ; inside   and want to split it to 2 strings by first occurrence of the ;. So, it should be: id and some text here with possible ; inside  I know how to split the string (for instance, with cut -d ';' -f1), but it will split to more parts since I have ; inside the left part.
6954
I'm trying to split text in a JTextArea using a regex to split the String by \n However, this does not work and I also tried by \r\n|\r|n and many other combination of regexes. Code:  public void insertUpdate(DocumentEvent e) {     String split[], docStr = null;     Document textAreaDoc = (Document)e.getDocument();      try {         docStr = textAreaDoc.getText(textAreaDoc.getStartPosition().getOffset(), textAreaDoc.getEndPosition().getOffset());     } catch (BadLocationException e1) {         // TODO Auto-generated catch block         e1.printStackTrace();     }      split = docStr.split("\\n"); }
24985
I saw , but I didn't see a JavaScript specific example. Is there a simple string.Empty available in JavaScript, or is it just a case of checking for ""?
51028
I'm splitting a string by three different characters but I want the output to include the characters I split by. Is there any easy way to do this?
113159
I'm not sure why I'm getting this error, but shouldn't this code compile, since I'm already checking to see if queue is getting initialized?   public static void Main(String[] args) {     Byte maxSize;     Queue queue;      if(args.Length != 0)     {         if(Byte.TryParse(args[0], out maxSize))             queue = new Queue(){MaxSize = maxSize};         else             Environment.Exit(0);     }     else     {         Environment.Exit(0);     }      for(Byte j = 0; j &lt; queue.MaxSize; j++)         queue.Insert(j);     for(Byte j = 0; j &lt; queue.MaxSize; j++)         Console.WriteLine(queue.Remove()); }   So if queue is not initialized, then the for loops aren't reachable right? Since the program already terminates with Environment.Exit(0)?  Hope ya'll can give me some pointers :)  Thanks.
34869
How can I iterate, loop, process, or otherwise do something, with each element from a comma separated list of things?  This question       , and the are already lots of very good answers everywhere. However I think it would also be valuable to have a single reference point where all such alternative techniques are explained in detail, with their advantages and disadvantages discussed.  All of plain-TeX, LaTeX, LaTeX3, LuaTeX, Context, etoolbox, pgffor, etc. answers are invited. Please consider explaining, for example:   How does the command/method deal with whitespace around items? How does the command/method deal with empty/missing items? (e.g. a,b,,c) How does the command/method deal with a trailing comma? Can the command/method work of a list stored on a macro? Is each iteration evaluated globally or on a local group? (i.e., if a command is defined during an iteration, does the command survive the loop?) Do items get expanded or somehow mangled before being processed? What about lists where items are key=value or some other kind of pairs? Does the method work for lists separated with something other than a comma? Any other thing I should know before deciding to use this method?
109620
I used   Runtime.getRuntime().exec("_____")   but it throws a IOException as below:  java.io.IOException: CreateProcess: c:/ error=5   at java.lang.Win32Process.create(Native Method)   at java.lang.Win32Process.&lt;init&gt;(Win32Process.java:63)   at java.lang.Runtime.execInternal(Native Method   I don't know whether I have the problem with specifying the path or something else. Can anyone please help me with the code.
2156
In Java is there a way to check the condition:  "Does this single character appear at all in string x"  without using a loop?
28013
Please consider the following MWE - where I try to test for string equality between an parameter, and a test string prepared by a macro:  \documentclass{article}  \def\testStr#1{IOIO#1}  \newcommand{\doTest}[1]{% \edef\tmphwp{\testStr{XX}} % \typeout{=\tmphwp=#1=} % \ifx\tmphwp#1{% \typeout{~equal}% }\else{% \typeout{!not equal}% }\fi % \typeout{ ^^J } % newline in terminal stdout }  \begin{document}  \doTest{12}  \doTest{hello}  \doTest{IOIOXX}  \end{document}   Strangely, this seems to always determine strings are unequal - even if I enter specifically the right string - terminal log (from pdflatex) reports:   =IOIOXX=12= !not equal   =IOIOXX=hello= !not equal   =IOIOXX=IOIOXX= !not equal   Hmm ... I would have expected IOIOXX = IOIOXX ?!   Can anyone explain why am I not getting the expected results - and how to get the right comparison? Does this have maybe to do anything with catcodes?  Many thanks in advance for any answers, Cheers!
116653
I have this:  If String.IsNullOrEmpty(editTransactionRow.pay_id.ToString()) = False Then     stTransactionPaymentID = editTransactionRow.pay_id 'Check for null value End If   Now, when editTransactionRow.pay_id is Null Visual Basic throws an exception. Is there something wrong with this code?