# String extraction

Page 1 of 1

## 7 Replies - 1828 Views - Last Post: 01 November 2009 - 01:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=135805&amp;s=64f821d9b093784071620bdc9b4841a6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 AmateurC

Reputation: -5
• Posts: 71
• Joined: 12-June 09

# String extraction

Posted 01 November 2009 - 02:53 AM

How can I use substring to to tell how many times the word "code" appears? ("d" can be substituted for any letter.)

countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2
Is This A Good Question/Topic? 0

## Replies To: String extraction

### #2 serafettin

• New D.I.C Head

Reputation: 7
• Posts: 30
• Joined: 21-June 09

## Re: String extraction

Posted 01 November 2009 - 03:02 AM

AmateurC, on 1 Nov, 2009 - 01:53 AM, said:

How can I use substring to to tell how many times the word "code" appears? ("d" can be substituted for any letter.)

countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2

well you can use a for loop for that.
```int countcode = 0;
String string = keyboard.next();
for(int i=0; i <= string.length()-4; i++)
{
if(string.substring(i,i+3) == "code")
countcode++;
}

```

This post has been edited by serafettin: 01 November 2009 - 03:04 AM

### #3 g00se

• D.I.C Lover

Reputation: 3225
• Posts: 14,230
• Joined: 20-September 08

## Re: String extraction

Posted 01 November 2009 - 03:04 AM

Use String.indexOf in a while loop

### #4 sakshamkum

Reputation: 19
• Posts: 232
• Joined: 09-June 09

## Re: String extraction

Posted 01 November 2009 - 06:51 AM

first you have an error:
if(string.substring(i,i+3)=="code")

will never work as Strings cannot be directly compred.

use:
if(string.substring(i,i+3).equalsIgnoreCase("code"))
{
}
also the substring goes till endinglimit -1 so you must have

```if(string.substring(i,i+4).equalsIgnoreCase("code"))
{
countcode++;
}

```

This post has been edited by sakshamkum: 01 November 2009 - 06:52 AM

### #5 serafettin

• New D.I.C Head

Reputation: 7
• Posts: 30
• Joined: 21-June 09

## Re: String extraction

Posted 01 November 2009 - 07:46 AM

sakshamkum, on 1 Nov, 2009 - 05:51 AM, said:

first you have an error:
if(string.substring(i,i+3)=="code")

will never work as Strings cannot be directly compred.

use:
if(string.substring(i,i+3).equalsIgnoreCase("code"))
{
}
also the substring goes till endinglimit -1 so you must have

```if(string.substring(i,i+4).equalsIgnoreCase("code"))
{
countcode++;
}

```

Yes sorry about that. I am a begginer in java to and I did not try my code I just write it. Thanks again for your correction

### #6 cdog5000

Reputation: 11
• Posts: 79
• Joined: 31-October 09

## Re: String extraction

Posted 01 November 2009 - 07:48 AM

```public static int count(String input, String search){
int count = 0;
for(int i = 0; i < input.length() - search.length(); i ++){
if(input.substring(i, search.length()).equals(search)){
count ++;
}
}
return count;
}
```

### #7 g00se

• D.I.C Lover

Reputation: 3225
• Posts: 14,230
• Joined: 20-September 08

## Re: String extraction

Posted 01 November 2009 - 08:27 AM

You can avoid all the overhead of substringing if all you want to do is get a count. Use indexOf

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: String extraction

Posted 01 November 2009 - 01:07 PM

serafettin, on 1 Nov, 2009 - 02:02 AM, said:

AmateurC, on 1 Nov, 2009 - 01:53 AM, said:

How can I use substring to to tell how many times the word "code" appears? ("d" can be substituted for any letter.)

countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2

well you can use a for loop for that.
```int countcode = 0;
String string = keyboard.next();
for(int i=0; i <= string.length()-4; i++)
{
if(string.substring(i,i+3) == "code")
countcode++;
}

```

if(string.substring(i,i+3) == "code")
should be
if(string.substring(i,i+3).equals("code"))

or it will never work