Shamoon
Shamoon

Reputation: 43491

How can I iterate over a string in Java?

public static Boolean cmprStr( String s1, String s2 )
{
    // STUFF
}

I want to iterate through s1 to make sure that every character in s1 is included in s2.

Upvotes: 8

Views: 60991

Answers (9)

user2125311
user2125311

Reputation: 67

// Here's some code I wrote to find CG ratio in a gene     
public double findCgRatio(String gene)
        {
            double cCount =0.0; 
            double gCount =0.0; 
            gene = gene.toLowerCase(); 
            for(char character : gene.toCharArray())
            {
                if(character == 'c')
                {
                    cCount++; 
                }
                else if(character == 'g')
                {
                    gCount++; 
                }

            }
            System.out.println("CG Ratio was :" + (cCount/gCount) );  
            return cCount/gCount;  // cgRatio 
        }

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

public static Boolean cmprStr( String s1, String s2 )
{
    for (int i = s1.length() - 1; i >= 0; --i) {
         if (s2.indexOf(s1.charAt(i)) == -1) {
             return Boolean.FALSE;
         }
    }
    return Boolean.TRUE;
}

Upvotes: 11

sjr
sjr

Reputation: 9875

All the other answers are O(n^2). Here's a way that is linear in time (i.e. O(n)) using Google Guava:

  public static boolean cmprStr(String s1, String s2) {
    Set<Character> desiredCharacters = Sets.newHashSet(Lists.charactersOf(s2));
    return Sets.difference(Sets.newHashSet(Lists.charactersOf(s1)), desiredCharacters).isEmpty();
  }

Upvotes: 3

Speck
Speck

Reputation: 2309

As I understand the question it would be.

//for each character in s1
  //if s2 does not contain character return false

//return true

for(int i = 0; i < length s1; i++){
  if(!s2.contains(String.valueOf(s1.charAt(i)))){
    return false;
  }
}
return true;

This verifies that each character in s1 is in s2. It does not confirm order, nor how many are there, and is not an equals method.

Recursive:

public static Boolean cmprStr( String s1, String s2 )
{
  if(s1.length() == 0 )
  {
    return true; 
  }
  if(!s2.contains(s1.substring(0,1)))
  {
    return false;
  }
  return cmprStr(s1.substring(1), s2);
}

Upvotes: 0

ColinD
ColinD

Reputation: 110046

Set<Character> charsInS1 = new HashSet<Character>();
for (int i = 0; i < s1.length(); i++) {
  charsInS1.add(s1.charAt(i));
}
for (int i = 0; i < s2.length(); i++) {
  charsInS1.remove(s2.charAt(i));
}
return charsInS1.isEmpty();

This has a complexity of O(n+m)... answers using indexOf have an O(n*m) complexity. It does of course use a bit of extra memory temporarily though.

Upvotes: 2

Eser Ayg&#252;n
Eser Ayg&#252;n

Reputation: 8004

Every String is also a CharSequence in Java. Therefore, you can easily iterate over a String using a simple for loop:

int n = s.length();
for (int i = 0; i < n; ++i) {
    char c = s.charAt(i);
    ...
}

Upvotes: 0

djna
djna

Reputation: 55907

length()

will give you the length of a string

charAt( someIndex)

will give you the character at a given position, so you can iterate the first String.

indexOf( achar )

will give you the poisition a char in a String, or -1 if it's not there. hence you should be able to look for each character in the first string within the second.

Upvotes: 10

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

  for(char c: s1.toCharArray()){
     if(s2.indexOf(c) == -1){
           return false;
     }
  }
  return true;

Assuming that

  s1 = "aabb";
  s2 = "ccddaannbbss";

will return true.

Upvotes: 11

jlink
jlink

Reputation: 692

Why don't you simply use 'equals' method ?

Boolean b = s1.equals(s2);

Upvotes: 1

Related Questions