如何获取字符串的内容减去ColdFusion中的扩展名? [英] How do I get the contents of a string minus the extension in ColdFusion?

查看:116
本文介绍了如何获取字符串的内容减去ColdFusion中的扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我只想在一个字段中的文件的文件名。说我有myimage.jpg我只想显示myimage我如何得到这一点?

For example, I want just the "filename" of a file in a field. Say I have myimage.jpg I only want to display "myimage" How do I get just that?

推荐答案

使用列表函数有利。

<cfset FileName = ListDeleteAt(FileFullName, ListLen(FileFullName, "."), ".")>

请注意,这仅适用于实际具有文件的文件名扩展(被定义为最后一个点之后的事)。为了更安全,以下是更好的:

Be aware that this only works for file names that actually have a file extension (that is defined as the thing after the last dot). To make it safer, the following is better:

<cfset ExtensionIndex = ListLen(FileFullName, ".")>
<cfif ExtensionIndex gt 1>
  <cfset FileExt  = ListGetAt(ExtensionIndex , ".")>
  <cfset FileName = ListDeleteAt(FileFullName, ExtensionIndex, ".")>
<cfelse>
  <cfset FileExt  = "">
  <cfset FileName = FileFullName>
</cfif>

更复杂的事情:可能有一些文件以点开头。可能存在包含许多相邻点的文件名。列表函数为它们返回错误的结果,因为它们忽略空列表元素。也可能有文件有点,但没有扩展名。只有提供扩展名白名单时,才能处理这些: ListFindNoCase(FileExt,doc,xls,ppt,jpg)。如果你想考虑所有这一切,你可能需要辞职到一个reguar表达式:

To complicate things a bit further: There may be files that start with a dot. There may be file names that contain many adjacent dots. List functions return wrong results for them, as they ignore empty list elements. There may also be files that have dots, but no extension. These can only be handled if you provide an extension white list: ListFindNoCase(FileExt, "doc,xls,ppt,jpg"). If you want to account for all of this, you probably need to resign to a reguar expression:

<cfset FileExtRe = "(?:\.(?:doc|xls|ppt|jpg))?$">
<cfset FileName  = REReplaceNoCase(FileExtRe, FileFullName, "")>

为了从路径拆分文件名,ColdFusion提供了不同的函数来处理平台差异: GetFileFromPath() GetDirectoryFromPath()

To split file name from path, ColdFusion provides distinct functions that also handle platform differences: GetFileFromPath() and GetDirectoryFromPath()

这篇关于如何获取字符串的内容减去ColdFusion中的扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆