5 Replies - 227 Views - Last Post: 07 February 2013 - 09:10 PM Rate Topic: -----

#1 Heiland  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 11-January 13

Java equivalent to "in" in python

Posted 07 February 2013 - 06:26 PM

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"?
Is This A Good Question/Topic? 0
  • +

Replies To: Java equivalent to "in" in python

#2 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,682
  • Joined: 13-March 10

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

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

Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Java equivalent to "in" in python

Posted 07 February 2013 - 06:51 PM

Note that chars have single quotes and Strings have double quotes. So darek9576's "+" should read '+'. There is also the String contains() method.
Was This Post Helpful? 2
  • +
  • -

#4 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

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

Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

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.
Was This Post Helpful? 1
  • +
  • -

#6 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

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.
Was This Post Helpful? 3
  • +
  • -

Page 1 of 1