取一个字符串前几位

来源:百度知道 编辑:UC知道 时间:2024/05/31 15:07:39
TCHAR *Cprint="12345.bmp";
TCHAR *Cprint1="126879.bmp";
TCHAR *Cprint2="3456879.bmp";
现在只需要“.bmp”前面的字符串,应该怎么截取啊!

反向 查找最后一个 '.'字符。

strrchr, wcsrchr
Scan a string for the last occurrence of a character.

char *strrchr( const char *string, int c );
char *wcsrchr( const wchar_t *string, int c );

示例:
char *tmp="12345.bmp";
char *pos=strrchr(tmp,'.');
if(pos!=NULL)
*pos='\0';

或者 CString 的 ReverseFind('.');

int ReverseFind( TCHAR ch ) const;

Return Value

The index of the last character in this CString object that matches the requested character; –1 if the character is not found.

示例:
CString tmp=CString(Cprint);
int pos=tmp.ReverseFind('.');
CString out=tmp.Left(pos+1);

SQL 取字符串的前几位数字,SQL 关键字 substring
substring 使用方法,参考下列SQL:
declare @T nvarchar(10)
set @T='12345abcde'
select substring(@T,1,5)
结果如下:12345
如果是SQL 写正则表达式判断,只能通过存储过程或函数来处理
SQL 如下:
CREATE FUNCTION d