如何从另一个字符串中删除最后一次出现的字符串?

发布于 2024-12-13 02:01:17 字数 513 浏览 0 评论 0原文

假设我必须从另一个字符串中删除一个字符串的最后一次出现。我该怎么办呢?

详细说明,我在 ac 字符串中有一个文件名( gchar* 或 char* )

C:\SomeDir\SomeFolder\MyFile.pdf

并且我想删除扩展名 .pdf ,并将其更改为其他内容,例如 .txt > 或 .png 。最省事又高效、方便、跨平台的方法是什么?谢谢。

注意:我知道在 C++ 中这是一件非常简单的事情,但对于这个项目,我绝对必须使用 C 而不是其他语言。 (学术要求)

注2:虽然你可以建议其他第三方库,但我目前只能访问C标准库和GLib。

注解3:我用“C”标签搜索了类似的问题,但似乎找不到。

Say I have to remove the last occurrence of a string from another string. How would I go about it?

To elaborate, I have a file name in a c string (gchar* or char* )

C:\SomeDir\SomeFolder\MyFile.pdf

and I want to remove the extension .pdf , and change it to something else, e.g .txt or .png . What is the least troublesome yet efficient , convenient and cross-platform way to do it? Thanks.

note: I know this is an extremely simple thing to do in C++ but for this project , I absolutely MUST use C and no other language. (academic requirement)

note 2: Although you can suggest other third-party library , I currently only have access to the C standard library and the GLib.

note 3: I have searched for similar questions with the "C" tag, but can't seem to find any.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

万劫不复 2024-12-20 02:01:17
char fil[] = "C:\\SomeDir\\SomeFolder\\MyFile.pdf";
char fil2[1000];
char extension[] = ".tmp";

// search for . and add new extension
sprintf(fil2, "%s%s", strtok(fil, "."), extension);
printf("%s\n", fil2);
char fil[] = "C:\\SomeDir\\SomeFolder\\MyFile.pdf";
char fil2[1000];
char extension[] = ".tmp";

// search for . and add new extension
sprintf(fil2, "%s%s", strtok(fil, "."), extension);
printf("%s\n", fil2);
月野兔 2024-12-20 02:01:17

我通常会使用“splitpath”函数来分隔完整路径名的所有四个部分(dir、path、name、ext)。
此致
奥利弗

I would normally use the "splitpath" function to seperate all four parts of a full path name (dir, path, name, ext).
Best regards
Oliver

撧情箌佬 2024-12-20 02:01:17

只需用“strrchr”函数修改上面的“strtok”代码

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

#define MAX_PATH 255

int main() 
{
  char f[MAX_PATH] = "dir\\file.pdf";
  char f1[MAX_PATH];
  char ext[] = ".tmp";
  char *ptr = NULL;
  // find the last occurance of '.' to replace
  ptr = strrchr(f, '.');
  // continue to change only if there is a match found
  if (ptr != NULL) {
    snprintf(f1, (ptr-f)+1, "%s", f);
    strcat(f1, ext);
  }
  printf("%s\n", f1);
  return 1;
}

Just modifying the above 'strtok' code with 'strrchr' function

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

#define MAX_PATH 255

int main() 
{
  char f[MAX_PATH] = "dir\\file.pdf";
  char f1[MAX_PATH];
  char ext[] = ".tmp";
  char *ptr = NULL;
  // find the last occurance of '.' to replace
  ptr = strrchr(f, '.');
  // continue to change only if there is a match found
  if (ptr != NULL) {
    snprintf(f1, (ptr-f)+1, "%s", f);
    strcat(f1, ext);
  }
  printf("%s\n", f1);
  return 1;
}
淤浪 2024-12-20 02:01:17

看看基本名称。

NAME
dirname, basename - Parse pathname components

SYNOPSIS
#include <libgen.h>

char *dirname(char *path);
char *basename(char *path);

DESCRIPTION
Warning: there are two different functions basename() - see below.
The functions dirname() and basename() break a null-terminated
pathname string into directory and filename components. In the
usual case, dirname() returns the string up to, but not including,
the final ’/’, and basename() returns the component following the
final ’/’. Trailing ’/’ characters are not counted as part of the
pathname.

have a look at basename.

NAME
dirname, basename - Parse pathname components

SYNOPSIS
#include <libgen.h>

char *dirname(char *path);
char *basename(char *path);

DESCRIPTION
Warning: there are two different functions basename() - see below.
The functions dirname() and basename() break a null-terminated
pathname string into directory and filename components. In the
usual case, dirname() returns the string up to, but not including,
the final ’/’, and basename() returns the component following the
final ’/’. Trailing ’/’ characters are not counted as part of the
pathname.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文