# Search and get position of a word in a comma separated string

**URL:** <https://discourse.processing.org/t/search-and-get-position-of-a-word-in-a-comma-separated-string/16671>\
**Category:** Beginners\
**Created:** [December 28, 2019, 4:18am UTC](https://discourse.processing.org/t/search-and-get-position-of-a-word-in-a-comma-separated-string/16671 "2019-12-28T04:18:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![molo32](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@molo32](https://discourse.processing.org/u/molo32)\
**Post date:** [December 28, 2019, 4:18am UTC](https://discourse.processing.org/t/search-and-get-position-of-a-word-in-a-comma-separated-string/16671/1 "2019-12-28T04:18:20Z")

</div>

suppose I have

`String s = "BANANA, APPLE, GRAPE"`

how to look for the word grape and return me position 2  
foo (s, “grape”) // return 2

If I look for the word apple, I should return the position 1  
If I look for the word banana, I should return the position 0

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 28, 2019, 4:24am UTC](https://discourse.processing.org/t/search-and-get-position-of-a-word-in-a-comma-separated-string/16671/2 "2019-12-28T04:24:37Z")

</div>

double post

> [@Find number in a text and return string from the row below](https://discourse.processing.org/t/find-number-in-a-text-and-return-string-from-the-row-below/16670):
>
> for example I have a text that contains 2 lines 1- “1000”, “55”, “123” 2- “hello”, “bye”, “good” I want a function that does this foo (“55”) this would have to find me the string “55” and return what is in the row below, in this case “bye” How do I do this without using Table?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 28, 2019, 11:41am UTC](https://discourse.processing.org/t/search-and-get-position-of-a-word-in-a-comma-separated-string/16671/3 "2019-12-28T11:41:06Z")

</div>

```auto
/**
 * Word Index Search (v1.0)
 * GoToLoop (2019/Dec/28)
 *
 * https://Discourse.Processing.org/t/
 * search-and-get-position-of-a-word-in-a-comma-separated-string/16671/3
 */

static final String
  FRUITS = "BANANA, APPLE, GRAPE", 
  FRUIT = "GRAPE", 
  DELIMS = ", ";

void setup() {
  final int idx = wordIndexSearch(FRUITS, FRUIT, DELIMS);
  println(idx); // 2
  exit();
}

static final int wordIndexSearch(
  final String words, 
  final String word, 
  final String delims) {

  if (words == null || word == null || words.isEmpty() || word.isEmpty())
    return -1;

  if (delims == null || delims.isEmpty())
    return int(word.equals(trim(words))) - 1;

  int idx = 0;

  for (final String w : splitTokens(words, delims))
    if (word.equals(w)) return idx;
    else ++idx;

  return -1;
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 28, 2019, 12:58pm UTC](https://discourse.processing.org/t/search-and-get-position-of-a-word-in-a-comma-separated-string/16671/4 "2019-12-28T12:58:04Z")

</div>

same as this

```auto

String
  FRUITS = "BANANA, APPLE, GRAPE", 
  FRUIT = "APPLE", 
  DELIMS = ", ";

void setup() {
  size(650, 650);
  background(0);

  int idx = wordIndexSearch(FRUITS, FRUIT, DELIMS);
  println(idx); // result

  text(idx, 111, 111);
}//func 

// ---------------------------------------------------------------------------

int wordIndexSearch(
  String words, 
  String word, 
  String delims) {

  // validate parameters 

  if (words == null || word == null || words.isEmpty() || word.isEmpty())
    return -1;

  if (delims == null || delims.isEmpty())
    return int(word.equals(trim(words))) - 1;

  // -------

  //search 

  // index 
  int idx = 0;

  for (String w : splitTokens(words, delims)) {
    if (word.equals(w)) {  
      return idx;
    }

    idx++;
  }

  // none found 
  return -1;
}//func 
//

```
