0

I have a project and i need to convert string (contain numbers and letters) to array

String name = "s111, s222, bbbb,cccc ";

and i want

array[0] = s111;
array[1] = s222;
array[2] = bbbb;
array[3] = cccc;

here is the code :

 String name = "s111, s222, bbbb,cccc ";
 int array[50];
 int r=0,t=0;

 for(int i=0;i<name.length();i++){
      if(name.charAt(i) == ','){
          array[t] = name.substring(r,i);
          r = (i+1);
          t++;
         }
 for(int k=0 ;k<=t ;k++){
   Serial.println(array[k]);
  }

When I compile I get just zeros like :

array[0] = 0;
array[1] = 0;
array[2] = 0;
array[3] = 0;
yo777
  • 23
  • 6
  • What exactly does not work? What did you expect and what actually happened? Please provide a full compilable code (this one does not have the declaration of the other variables in it, including the array). Also my answer to [this question](https://arduino.stackexchange.com/questions/77081/arduino-split-comma-separated-serial-stream/77082#77082) might also be helpful. – chrisl Jul 26 '20 at 19:54
  • why are you asking us to guess what your code actually does? ... we can't even test your code because you provided code that does not compile ... your description of the problem, `it wont work`, it useless – jsotola Jul 26 '20 at 19:58
  • I think the description is clear – yo777 Jul 26 '20 at 20:03
  • Re “i want `array[0] = s111;`”: this doesn't make sense. `array` is an array of `int`s, and “s111” is not an `int`. – Edgar Bonet Jul 26 '20 at 20:06
  • thanks but do you have another way to split the character of that variable – yo777 Jul 26 '20 at 20:12
  • Do you need an array of strings or an array of int? If int, how should the last 2 parts be interpreted? ASCII hex? – chrisl Jul 26 '20 at 21:00
  • array of strings – yo777 Jul 26 '20 at 21:56
  • 1
    "I think the description is clear." You are wrong. Your question is a muddy mess. You haven't provided enough code for us to test your program. You haven't told us what goes wrong with your current code. You haven't told us what format your output is supposed to be in (you finally said "array of strings" in the comments, but you need to edit your question to provide a full description of the problem in the question itself. Don't expect somebody trying to help you to read through an endless question-and-answer session to figure out what you are really asking. – Duncan C Jul 26 '20 at 23:42

1 Answers1

0

When you declare int array[50]; you declare an array of numbers so only numbers can be stored inside it. If you really want an array of strings then declare an array of strings like String array[50]; There was also a a problem that sometime you split the name by comma, sometime by space, and sometime by comma+space. Also your code was missing closing bracket.

The algorithm has to split both by comma and my space and then take only values that have length more than one to avoid inserting empty strings.

String name = "s111, s222, bbbb,cccc ";
String array[50];
int r=0,t=0;
  
for(int i=0;i<name.length();i++)
{
  if(name[i] == ' ' || name[i] == ',')
  {
    if (i-r > 1)
    {
      array[t] = name.substring(r,i);
      t++;
    }
    r = (i+1);
  }
}

for(int k=0 ;k<=t ;k++)
{
  Serial.println(array[k]);
}
Filip Franik
  • 1,272
  • 1
  • 7
  • 20