首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从完整文件名中提取文件名

从完整文件名中提取文件名是一个常见的任务,可以使用编程语言中的字符串处理函数来实现。以下是几种常见编程语言中的实现方法:

  1. Python
代码语言:python
复制
import os

filename = 'example.txt'
full_path = '/path/to/file/' + filename
file_name_only = os.path.basename(full_path)
print(file_name_only)
  1. Java
代码语言:java
复制
import java.io.File;

public class Main {
    public static void main(String[] args) {
        String fullPath = "/path/to/file/example.txt";
        File file = new File(fullPath);
        String fileNameOnly = file.getName();
        System.out.println(fileNameOnly);
    }
}
  1. JavaScript (Node.js)
代码语言:javascript
复制
const path = require('path');

const fullPath = '/path/to/file/example.txt';
const fileNameOnly = path.basename(fullPath);
console.log(fileNameOnly);
  1. C#
代码语言:csharp
复制
using System;
using System.IO;

class Program {
    static void Main() {
        string fullPath = "/path/to/file/example.txt";
        string fileNameOnly = Path.GetFileName(fullPath);
        Console.WriteLine(fileNameOnly);
    }
}

这些代码示例都使用了各自编程语言中的字符串处理函数来从完整文件名中提取文件名。在实际应用中,可以根据需要进行相应的修改和调整。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券