In python you could do something like:
someString= "2 + 3"
if "+" in someString:
do things
else:
do other things
what is the java equivalent to "in"?
Java equivalent to "in" in python
Page 1 of 15 Replies - 138 Views - Last Post: 07 February 2013 - 09:10 PM
Replies To: Java equivalent to "in" in python
#2
Re: Java equivalent to "in" in python
Posted 07 February 2013 - 06:33 PM
You have to loop through the string and use ==.
e.g
e.g
for(int i-0; i < someString.length; i++)
{
if(someString.charAt(i) == "+"){
//do something
}
}
This post has been edited by darek9576: 07 February 2013 - 06:34 PM
#4
Re: Java equivalent to "in" in python
Posted 07 February 2013 - 07:25 PM
for(int i=0; i < someString.length(); i++)
{
System.out.println((someString.substring(i,i+1)));
if(someString.substring(i,i+1).equals("+"))
{
//do something
System.out.println(true);
}
else System.out.println(false);
}
This post has been edited by burakaltr: 07 February 2013 - 07:28 PM
#5
Re: Java equivalent to "in" in python
Posted 07 February 2013 - 09:05 PM
Use of the substring() method is really a bad choice here, as substring() returns a String. If that String doesn't already exist in the String pool, then a new one is created. Note that if "123" exists in the String pool, that doesn't guarantee that "12" will exist there. The charAt() method is a better choice because chars are primitives, and the String class just returns the char from the given index in the array.
#6
Re: Java equivalent to "in" in python
Posted 07 February 2013 - 09:10 PM
I just wanted to offer an alternative .
Thanks for being explanative and intuitive.
Thanks for being explanative and intuitive.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote








|