# Breaking up a String

**URL:** <https://discourse.processing.org/t/breaking-up-a-string/32939>\
**Category:** Coding Questions\
**Created:** [October 19, 2021, 6:35pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939 "2021-10-19T18:35:22Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [October 19, 2021, 6:35pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/1 "2021-10-19T18:35:22Z")

</div>

Hello All,

So I’m messing around with some Fantasy Football data for a little coding project. I have a spread sheet that has the following information from a daily fantasy football contest I played in.

A full lineup of players (1 QB, 2 RB, 3 WR, 1 TE, 1 FLEX (can be a RB WR or TE) and a Defense)  
Where that lineup finished in the contest.  
The total number of points scored by that lineup.

I have a few ideas for some data analysis I would like to do, but I need to break up the lineups a little differently. Currently each String element in the “Lineups” tab in the spread sheet looks like this:

`String = "QB Dak Prescott RB Darrell Henderson Jr. RB Darrel Williams FLEX J.D. McKissic WR Cooper Kupp WR CeeDee Lamb WR Adam Thielen TE Ricky Seals-Jones DST Colts"`

The general format is “Position FirstName LastName…repeat”

My idea was to use something like indexOf() to find the instances of each of the positions in the String, then use those results to get a substring with each player and their corresponding positions.

I know indexOf() only works for the first instance of a position. So if I did:

`String.indexOf("RB")`

on the existing string it would only give me the first instance of those letters occurring. My thought is I could run a loop where indexOf() finds a position index, I take out that one position I need, then on the remaining string rerun indexOf() in order to break each element I need out of the first string.

My question, is there an existing function that does this and I’m just making a bunch of work for myself for no reason? Or, is there potentially a better logical way to go about this problem that I’m not using?

Thank you all as always for the help.

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [October 19, 2021, 6:57pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/2 "2021-10-19T18:57:44Z")

</div>

> **[Reference](https://processing.org/reference/split_.html)**
>
> The split() function breaks a String into pieces using a character or string as the delimiter. The delim parameter specifies the character or characters that mark the boundaries between …

> **[Reference](https://processing.org/reference/splitTokens_.html)**
>
> The splitTokens() function splits a String at one or many character delimiters or "tokens". The delim parameter specifies the character or characters to be used as a boundary.

Maybe this could help?  
Not 100% sure but it should be useful

---

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [October 19, 2021, 7:15pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/3 "2021-10-19T19:15:11Z")

</div>

This is very helpful ty!

I knew there were other functions I just haven’t used as much built into Processing. These both seem like they would be helpful to understand for this project.

---

<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:** [October 19, 2021, 7:25pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/4 "2021-10-19T19:25:50Z")

</div>

> [@dtools22](#):
>
> The general format is “Position FirstName LastName…repeat”

Do you mean that word “repeat”:

- For one team / game OR
- can there be multiple games in a String…?

---

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [October 19, 2021, 8:28pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/5 "2021-10-19T20:28:23Z")

</div>

I mean that each string repeats that pattern. Each lineup string is from a single week of NFL games and consists of only the players that were used by that lineup entry.

A little more detail.

I have found out from playing around a little bit that the format for these Strings is to have the position followed by the player’s first and then last name. I also found that the positions are listed in this order

QB, RBs, WRs, TEs, DST

So all RB positional players will come after the QB and before the WRs. The interesting variable is the FLEX position which can be a RB, WR, or TE. I found the FLEX will go with whatever positional grouping the player belongs to. So for example, in the original post I used the string:

“QB Dak Prescott RB Darrell Henderson Jr. RB Darrel Williams FLEX J.D. McKissic WR Cooper Kupp WR CeeDee Lamb WR Adam Thielen TE Ricky Seals-Jones DST Colts”

The ‘FLEX J.D McKissic’ is listed with the other RBs because that is his position on the field, but because he’s the third RB in this lineup he goes in the FLEX position.

So ideally I would like to take this string and be able to turn it into 9 substrings.  
QB Dak Prescott  
RB Darrell Henderson Jr.  
RB Darrel Williams  
FLEX J.D. McKissic  
WR Cooper Kupp  
WR CeeDee Lamb  
WR Adam Thielen  
TE Ricky Seals-Jones  
DST Colts

Which will make analyzing the data a little easier since the spreadsheet that my orignial data is from has 580,000+ lineup entries.

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [October 19, 2021, 8:54pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/6 "2021-10-19T20:54:54Z")

</div>

I also think `split` is a good starting point - then you break down the long string to an array of strings, with delimiter `' '`.

Then I guess you have to hardcode position names because sometimes the name is more than 2 tokens - not only `first last` but sometimes like `first last jr` and in this case you cannot just iterate over the array every `i+=3`, assuming `i` is position, `i+1` is first and `i+2` is last name. iterating over the split array would be something like

1. is this token matches a position name? (e.g., `QB`?)
  - if yes, update the current position, clear the name
  - if no, add this as part of the name of the current position

2. increment `i`

---

<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:** [October 19, 2021, 9:09pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/7 "2021-10-19T21:09:15Z")

</div>

> [@dtools22](#):
>
> since the spreadsheet that my orignial data is from

are you familiar with loadTable etc.?

here you can load a csv

---

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [October 19, 2021, 9:13pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/8 "2021-10-19T21:13:50Z")

</div>

I am, and have actually done a few programs loading the table directly into the sketch. My plan in general is to do that with this project as well, I just need the column where the Lineup String files will be in to be broken down a little differently.

Ideally, each position gets its own cell rather than one cell having all nine players.

I want to eventually get to a point where I can track certain combinations of players. As an example, one of the strategies to the game is to “stack” players, meaning intentionally pick a QB and WR from the same team so that when one does well there is a better chance both do well. I want to analyze parings like this to better understand how effective this strategy and others like it ultimately are.

---

<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:** [October 19, 2021, 9:45pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/9 "2021-10-19T21:45:20Z")

</div>

> [@dtools22](#):
>
> Ideally, each position gets its own cell rather than one cell having all nine players.

…

can you post a bit from your spreadsheet (as csv)

or screenshot?

I doubt they are in ONE cell somehow…

anyway please go back to CodeMasterX’s post please

---

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [October 19, 2021, 9:57pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/10 "2021-10-19T21:57:47Z")

</div>

Here are the first few lines of the .csv file

```auto
Rank,EntryId,EntryName,TimeRemaining,Points,Lineup,,Player,Roster Position,%Drafted,FPTS
1,2889749269,delmar3 (4/5),0,239.1,QB Dak Prescott RB Darrell Henderson Jr. RB Darrel Williams FLEX J.D. McKissic WR Cooper Kupp WR CeeDee Lamb WR Adam Thielen TE Ricky Seals-Jones DST Colts ,,Darrell Henderson Jr.,RB,27.77%,24.7
2,2899578667,scotty1737,0,238.02,QB Kirk Cousins RB Jonathan Taylor RB Joe Mixon WR Cooper Kupp WR Adam Thielen WR Henry Ruggs III FLEX Donovan Peoples-Jones TE Mark Andrews DST Cowboys ,,Kareem Hunt,FLEX,27.01%,10.8
3,2899753959,wundyfull,0,237.09998,QB Dak Prescott RB Jonathan Taylor RB Joe Mixon FLEX Khalil Herbert WR CeeDee Lamb WR Courtland Sutton WR Adam Thielen TE Noah Fant DST WAS Football Team ,,Jonathan Taylor,RB,25.97%,31.8
4,2895380167,Gabbie (1/7),0,232.04002,QB Matthew Stafford FLEX Jonathan Taylor RB Darrell Henderson Jr. RB Darrel Williams WR Cooper Kupp WR Adam Thielen WR Donovan Peoples-Jones TE Hunter Henry DST Rams ,,Ja'Marr Chase,WR,25.61%,13.7
5,2899639029,bullnasty,0,231.85999,QB Baker Mayfield RB Jonathan Taylor RB Joe Mixon WR CeeDee Lamb WR Adam Thielen WR Donovan Peoples-Jones FLEX Travis Kelce TE Noah Fant DST Colts ,,Mark Andrews,TE,24.99%,17.8

```

There is a little noise after the “Lineup” column. This .csv file contains both the lineup rankings from the contest and some specific data for each plyer that was used in all lineups. I am mostly interested in the information up through the “Lineup” column for this project.

---

<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:** [October 19, 2021, 9:59pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/11 "2021-10-19T21:59:49Z")

</div>

ty

What is

```auto
,,Mark Andrews,TE,

```

in the last line please?

---

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [October 19, 2021, 10:04pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/12 "2021-10-19T22:04:13Z")

</div>

That is a data point from the second column after the “Lineup” column. Each line for the first few hundred has some additional columns of information that pertain to how each individual player was used in the contest and their results.

Player  
Mark Andrews

Position  
TE

Drafted (percentage of lineups this player appears in)  
24.99%

FTP  
17.8

---

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [October 19, 2021, 10:11pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/13 "2021-10-19T22:11:05Z")

</div>

![Post picture](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9405699f6c510fcafcaa17b723580a3042cbb55c.png)

Here is a Picture of what the top 10 lines look like if you take out those last few column after the “Lineup” column

---

<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:** [October 19, 2021, 10:26pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/14 "2021-10-19T22:26:28Z")

</div>

here

```auto

String in1 = "QB Dak Prescott RB Darrell Henderson Jr. RB Darrel Williams FLEX J.D. McKissic WR Cooper Kupp WR CeeDee Lamb WR Adam Thielen TE Ricky Seals-Jones DST Colts";

// list of possible delimiters
String[] del1 = { 
  "QB", 
  "RB", 
  "FLEX", 
  "WR", 
  "TE", 
  "DST"
}; 

void setup () {
  size(233, 233);

  String[] resultMy1 = hitIt(in1);
  for (String s1 : resultMy1) { 
    println(s1);
  }
}

// crunch the initial String 
String[] hitIt(String in) {

  String[] result={}; // 

  // while String is left we crunch
  while (in.length()>0) {

    //println(in);

    // search start position of a player  
    int min1 = getIndex (in, 0 ) ;  
    // println("min1 is " + min1); 

    // Found?
    if (min1<1111) {
      // search end position of the player
      int min2 = getIndex ( in, min1+2 ) ;

      // found?
      if (min2<1111) { 
        // println( in.substring(min1, min2).trim());
        result = (String[]) append (result, in.substring(min1, min2).trim());
      } else {
        // we assume it's the last entry (substring goes to the end of the remaining String)
        result = (String[]) append (result, in.substring(min1).trim());
      }
      // println(result[index]);

      // cut the remaining String
      if (min2<1111) { 
        in = in.substring(min2);
      } else {
        // END (?)
        in = "";
      }
    }
  }

  return 
    result;
} 

int getIndex ( String in, int start1 ) {
  // returns the smallest next position of any delimiter 
  int min1=1111;
  String bestDel; 

  for (String delMy : del1) {
    int c1 = in.indexOf(delMy, start1);
    if (c1>-1)
      if (c1<min1) {
        min1=c1; 
        // bestDel=delMy;
      }
  }
  return min1;
}
//

```

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [October 20, 2021, 12:56am UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/15 "2021-10-20T00:56:35Z")

</div>

I noticed that in the Lineup string, the players are separated by spaces, so I would delete all of those from the string. You would loop through the characters in the string to look for any key positions like QB or RB or WR, and if there are, you would make a new string with the position info and the player name (list of characters until the next position. These positions are all in order, so you can kind of hard-code this.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 20, 2021, 1:00am UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/16 "2021-10-20T01:00:54Z")

</div>

Hello,

> [@dtools22](#):
>
> So ideally I would like to take this string and be able to turn it into 9 substrings.

I used some brute force on this to change delimiters:

```auto
// String manipulation
// v1.0.0
// GLV 2021-10-19

String s1 = "1,2889749269,delmar3 (4/5),0,239.1,QB Dak Prescott RB Darrell Henderson Jr. RB Darrel Williams FLEX J.D. McKissic WR Cooper Kupp WR CeeDee Lamb WR Adam Thielen TE Ricky Seals-Jones DST Colts ,,Darrell Henderson Jr.,RB,27.77%,24.7";
String ss1 [];
String ss2 [] = new String[9];
String dlms [] = {"QB", "RB", "FLEX", "WR", "TE", "DST"};
String s2 = s1; //Working copy

void setup() 
	{
  println(s2); 
  println(); 
  
  // This can go neatly in a loop:
  s2 = s2.replace("QB", ",QB");
  s2 = s2.replace("RB", ",RB");
  s2 = s2.replace("FLEX", ",FLEX");
  s2 = s2.replace("WR", ",WR");
  s2 = s2.replace("TE", ",TE");
  s2 = s2.replace("DST", ",DST");
  s2 = s2.replace(",,", ",");
  
  // Split and trim
  ss1 = trim(split(s2, ","));
  printArray(ss1);
  println(); 

  //Make a copy 
  arrayCopy(ss1, 5, ss2, 0, 9); 
  printArray(ss2); 
  println();
  }

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/1/14683ea34ca19b32abf20ea4d4f05e5e67b8500f.png)

`:)`

---

<div class="post-metadata">

**Author:** ![dtools22](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dtools22](https://discourse.processing.org/u/dtools22)\
**Post date:** [October 20, 2021, 11:58pm UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/17 "2021-10-20T23:58:29Z")

</div>

These are some great solutions, thank you all for the help!

In short, I’m trying to get a little better at text based data manipulation so this has been a big help. Thank you all again!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 21, 2021, 11:34am UTC](https://discourse.processing.org/t/breaking-up-a-string/32939/18 "2021-10-21T11:34:34Z")

</div>

Hello,

Lots of resources here:  
[https://processing.org](https://processing.org) \< Explore away!

Some good tutorials in the _Learn_ section.

This one seems to be missing a link on the new site on _Tutorial_ page but does:

> **[Data](https://processing.org/tutorials/data/)**
>
> Learn the basics of working with data feeds in Processing.

Have fun!

`:)`
