[Linux C] linux C实现trim功能

Linux C中没有提供trim功能的API。

实际编程过程中,很多地方会使用到类似trim功能的地方。

比如: 从文件中读取一行数据,存到字符串中。

但从文件读取的数据的末尾会包含 “\r\n”(windows文件格式)或者“\n”(linux 文件格式)

或者读取的数据前面会有空格和\t

但往往我们所关心的数据并不包含这些\r或者\n的字符串。

下面是自己编写的一个trim函数。

实现如下功能:

1.  提供三种模式

1.1 只从字符串首开始检测并删除指定字符

1.2 只从字符串尾部开始检测并删除指定字符

1.3 同时从字符串首部和尾部检测并删除指定字符

2.  可以自己指定所要trim的字符,可以指定多个字符

相关Code:

1. 函数原型如下所示:

void trim(char *source_string, const char *trim_string, trim_direction_mode_t trim_mode)

其中:

参数 source_string是需要进行trim操作的原始字符串

参数 trim_string是指定的需要在原始字符串中trim的字符

参数 trim_mode是指定trim的模式

其中 trim_direction_mode_t是自己定义的trim的模式的枚举,code如下:

enum _trim_direction_mode
{
	E_TRIM_MODE_NONE = 0x0,
	E_TRIM_MODE_FRONT = 0x1,
	E_TRIM_MODE_BACK = 0x2,
	E_TRIM_MODE_ALL = 0x3,
};
typedef enum _trim_direction_mode trim_direction_mode_t;
顾名思义,定义了4种模式:

E_TRIM_MODE_NONE <-- 不进行trim操作

E_TRIM_MODE_FRONT <-- 只对原始字符串的行首进行trim操作

E_TRIM_MODE_BACK <-- 只对原始字符串的行尾进行trim操作

E_TRIM_MODE_ALL<-- 对原始字符串既进行行首操作也进行行尾操作

2. trim函数code如下:

static void trim(char *source_string, const char *trim_string, trim_direction_mode_t trim_mode)
{	
	int input_len = strlen(source_string);
	int trim_string_len = strlen(trim_string);
	char *input_value_ptr = source_string;
	const char *trim_string_ptr = trim_string; 

	int escape_flag = FALSE;
	int i = 0, j = 0;

	assert(source_string != NULL);
	assert(trim_string != NULL);

	if((trim_mode & E_TRIM_MODE_FRONT) != 0)
	{
		/* delete the front character */
		for (i = 0; i < input_len; i++)
		{
			/* cycle the trim string, if the character isn't one of trim string, 
			 * escape the cycle, and move the string */
			for (j = 0; j < trim_string_len; j++)
			{
				if (input_value_ptr[i] == trim_string_ptr[j])
				{
					break;
				}
				if(j == (trim_string_len - 1))
				{
					escape_flag = TRUE;
				}
			}	
			if(escape_flag)
			{
				break;
			}
		}	
		memmove(input_value_ptr, input_value_ptr+i,(input_len - i + 1)); 
	}	

	if((trim_mode & E_TRIM_MODE_BACK) != 0)
	{
		input_len = strlen(input_value_ptr);
		escape_flag = FALSE;
		/* delete the end character */
		for (i = input_len - 1; i >= 0; i--)
		{
			/* cycle the trim string, if the character isn't one of trim string, 
			 * escape the cycle, and move the string */
			for (j = 0; j < trim_string_len; j++)
			{
				if (input_value_ptr[i] == trim_string_ptr[j])
				{
					break;
				}
				if(j == (trim_string_len - 1))
				{
					escape_flag = TRUE;
				}
			}	
			if(escape_flag)
			{
				break;
			}
		}	
		input_value_ptr[i+1] = '\0';
	
	}

	return;
}
函数作用就是按照trim mode,从trim_string中选取要trim的字符,对source_string进行trim操作。
其中,TRUE和FALSE是自己定义的boo变量

#ifndef BOOL
#define BOOL int
#define TRUE 1                                                                                                                                              
#define FALSE 0
#endif


代码执行示例:

int main(int argc, char *argv[])
{
    
    char string[100] = "abcdefghijklmn";
    char tmp[100] = ""; 

    strncpy(tmp, string, sizeof(tmp));
    printf("source string is : %s\n", tmp);
    trim(tmp, "abmn", E_TRIM_MODE_FRONT);   
    printf("trim front string is : %s\n", tmp);


    strncpy(tmp, string, sizeof(tmp));
    printf("source string is : %s\n", tmp);
    trim(tmp, "abmn", E_TRIM_MODE_BACK);    
    printf("trim back string is : %s\n", tmp);
    

    strncpy(tmp, string, sizeof(tmp));
    printf("source string is : %s\n", tmp);
    trim(tmp, "abmn", E_TRIM_MODE_ALL); 
    printf("trim front and back string is : %s\n", tmp);

    return 0                                                                                                                                                

}

运行结果如下:
source string is : abcdefghijklmn
trim front string is : cdefghijklmn
source string is : abcdefghijklmn
trim back string is : abcdefghijkl
source string is : abcdefghijklmn
trim front and back string is : cdefghijkl

转载请注明出处:http://blog.csdn.net/jibing57/article/details/7448692
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值