Skip to main content

contains() and indexOf() in Java will return true for Empty String("")

In Java we will use String.contains(String2) to check a String contains another String. This Contains() method will return true if we String2 is empty that means "". Also String.indexOf(String2) and String2 is empty "", this also will return 0. If we check return index is greater than -1 or not equal to -1, this will return true. So to avoid this, we have to check the given string is not empty.


                String s = "";
String b = "test";
System.out.println("b contains s?:"+b.contains(s)); //true
System.out.println("b indexOf s?:"+b.indexOf(s)); //0
System.out.println("b indexof 't'?:"+b.indexOf("t")); //0
System.out.println("s contains b?:"+s.contains(b)); //false


Do not check index or contains for empty String at any time. 

Comments

Popular posts from this blog

remove(unset) property in a property file using ANT PropertyFile task

Using PropertyFile task we can edit the property file during ANT build. Edit property value are very easy and can find here details. http://ant.apache.org/manual/Tasks/propertyfile.html but delete a property or comment a property is the tricky one. If we are using latest ant version (1.8.1 or later), we can delete a property like below. <propertyfile file="my.properties" comment="My properties">   < entry  key="propertykey" operation="del"/> < /propertyfile> but the older version that is before 1.8.1 don't have operation called " del ", if we run the command in lower version ant, we will get a error says undefined operation "del".      there is workaround instead deleting a property we can comment that property using ant's replace command. < replace file="sample.properties">                      < replacefilter token="propertykey" value="#propert

Running and debugging jetty maven plugin in Netbeans

Coding with Netbeans will make developer life simple. We don't need search for plugins and install different plugins from different places. Maven projects can opened as directly in Netbeans without any settings. Here is the way to run & debug using maven jetty plugin. We can't add Jetty server as servers in Netbeans. Run project: Right click on project and open the properties. click on "Actions" on left side. On right side there will be list of available actions.  To run jetty can be created a new action or override default "Run Project" Here I am overriding default one, Because I can run the project just by hitting "F6" key click on "Run Project" Enter the value "Execute Goals" as "jetty:run", If you want run as offline add "jetty:run -o" "Active Profiles" can be empty In "set Properties" field we can set like skip test. Here I am skipping test. Done, After setting y

Change default JAVA_OPTS in JBoss 7

To change JVM memory options in JBOSS 7 for standalone server, find the file standalone.conf.bat in bin directory and edit the file like below. rem # JVM memory allocation pool parameters - modify as appropriate. set "JAVA_OPTS=-Xms3072M -Xmx3072M -XX:MaxPermSize=1024M"