Get substring of string using C

dulan

Say I have a const char* string like this:

../products/product_code1233213/image.jpg

I want to retrieve the second last part of this path string, which is the parent folder name of the jpg file, how would I do that?

Jayesh Bhoi

You can use strtok.

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

int main()
{
   char str[] = "/products/product_code1233213/image.jpg";
   char s[2] = "/";
   char *token;

   /* get the first token */
   token = strtok(str, s);

   /* walk through other tokens */
   while( token != NULL ) 
   {
      printf( " %s\n", token );

      token = strtok(NULL, s);
   }

   return(0);
}

Output:

products
product_code1233213
image.jpg

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

get substring from string Java(Android)

분류에서Dev

Get substring location within a given string

분류에서Dev

C # 구분 List <string> by substring

분류에서Dev

Extract a substring after some string in C#

분류에서Dev

How to check if number exists after a substring in string using javascript?

분류에서Dev

Finding Substring from a String

분류에서Dev

C #에서 string.Split () 대 string.Substring ()의 효율성?

분류에서Dev

C++ substring check

분류에서Dev

Input string without using C++ string

분류에서Dev

String.Substring 설명

분류에서Dev

How to get left substring in mysql

분류에서Dev

Get id from string using javascript

분류에서Dev

get string from file name using KSH

분류에서Dev

Getting substring using ksh script

분류에서Dev

Getting substring using ksh script

분류에서Dev

Using regex to find substring in Java

분류에서Dev

Using Charindex with in Substring is resulting in error

분류에서Dev

Get javascript value to c# string

분류에서Dev

Substring extraction from a given string, Oracle

분류에서Dev

getting a substring from an unknown string in java

분류에서Dev

Cut a Substring of a String which was read by serial in Python

분류에서Dev

How to extract substring from a string in ColdFusion?

분류에서Dev

Finding location of specified substring in a specified string (MATLAB)

분류에서Dev

Ordering a string by its substring numerical value in python

분류에서Dev

Javascript: get username substring(s) beginning with @ symbol

분류에서Dev

Using NCHAR to get special characters in C#

분류에서Dev

Get Video File and save using c#

분류에서Dev

substring in awk using gsub regular expression

분류에서Dev

TypeError when using substring function in Python 3