-3

I know that is a simple question but I couldn't find the answer.

I have this string:

"M1[r2][r3]"

I want to get only the "M1", I'm looking for something like strchr() a function that get the string and a char to stop at.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Elon Salfati
  • 1,537
  • 6
  • 23
  • 46

4 Answers4

9

What about using strtok and "[" as a delimiter?

#include <string.h> /* for strtok */
#include <stdio.h>  /* for printf */
int main()
{
    char str[] = "M1[r2][r3]"; // str will be modified by strtok
    const char deli[] = "[";   // deli could also be declared as [2] or as const char *. Take your pick...
    char *token;

    token = strtok(str, deli); // can also call strtok(str, "["); and not use variable deli at all
    printf("%s", token);       // printf("%s", str); will print the same result

    /* OUTPUT: M1 */

    return 0;    
}
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
  • 2
    You should mention that `strtok()` changes the input buffer so that printing `str` will be `"M1"` too because every separator will be replaced by a null terminator. Further no `[2]` is needed use just `[]`. – Andre Kampling Aug 23 '17 at 06:57
  • I don't get why this answer get downvotes. Yes it isn't a "write the code for me" site but the answer isn't wrong or lacks in quality. I would say if somebody isn't comfortable with an answer to such a question he/she shouldn't upvote but also not downvote. – Andre Kampling Aug 23 '17 at 07:02
  • 1
    I agree that `[]` will be ok, but why not use `[2]`? I think of it as a reminder (especially for beginners) that "[" is a string literal that has 2 `char` – CIsForCookies Aug 23 '17 at 07:03
  • 1
    @ClsForCookies: Ah I didn't saw it that way :D. I was just thinking don't repeat yourself. – Andre Kampling Aug 23 '17 at 07:04
  • @AndreKampling it is usually considered [**unhelpful**](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) to supply a pat answer to "gimme the code" questions. – Weather Vane Aug 23 '17 at 07:05
  • 1
    I hope I'm not overstepping my bounds, but from my experience, when I was doing my first steps here at SO, these were the answers that were most helpful for me. I got to learn about new functions and how someone else would achieve my question goal. If the OP wasn't a noob here, my answer would be different – CIsForCookies Aug 23 '17 at 07:07
  • 1
    @CIsForCookies nice answer, +1, but please include the headers needed, some users might not be aware of them (although they can see it in the ref, but...). – gsamaras Aug 23 '17 at 08:41
2

Use strtok() and the character to stop (a delimeter, in your case [) like this:

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="M1[r2][r3]";
  printf ("Getting M1 from \"%s\":\n",str);
  strtok (str,"[");
  if (str != NULL)
    printf ("%s\n",str);
  return 0;
}

Output:

Getting M1 from "M1[r2][r3]":
M1

However, if you know the length of the substring, you should see Get a substring of a char*.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
2

something like strchr() a function that get the string and a char to stop at.

If only a printing of the sub-string is needed, use strcspn() which finds the offset when '[' first occurs, (or the end if not found). This does not alter the source string.

const char *s = "M1[r2][r3]";
printf("%.*s\n", (int) strcspn(s, "["), s);

Output

M1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

Use sscanf() like this:

char subStr[3];
sscanf("M1[r2][r3]", " %2[^[]", subStr);

where [^[] means a character except [ and 2 guards the length of the string written into the substring (it's equal to subStr's size - 1, so that space for the NULL terminator is available).

as BLUEPIXY suggested.

gsamaras
  • 71,951
  • 46
  • 188
  • 305