1、trim()方法返回調(diào)用字符串對象的一個副本,但是所有起始和結(jié)尾的空格都被刪除了,例子如下:String s=" Hello World ".trim();就是把"Hello World"放入s中。
專注于為中小企業(yè)提供網(wǎng)站制作、成都網(wǎng)站制作服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)通化免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
2、例程:
void?trim(char*?s,?char?c)
{
char?*t??=?s;
while?(*s?==?c){s++;};
if?(*s)
{
char*?t1?=?s;
while?(*s){s++;};
s--;
while?(*s?==?c){s--;};
while?(t1?=?s)
{
*(t++)?=?*(t1++);
}
}
*t?=?0;
}
int?main()
{
char?mm[]?=?"**hello?word!**";
trim(mm,?'*');
printf("%s\n",?mm);
}
c語言去掉字符串的空格函數(shù) void trim(char *s){} 如下:
#include stdio.h
void trim(char *s){
int i,L;
L=strlen(s);
for (i=L-1;i=0;i--) if (s[i]==' ')strcpy(s+i,s+i+1);
}
int main(){
char s[100];
printf("input 1 line string\n");
gets(s);
trim(s);
printf("%s\n",s);
return 0;
}
例如:
input 1 line string
abc 123 XYZ |
輸出:abc123XYZ|
#includestdio.h
#define?M?1024
void?main()?{?int?i,j,n,nn,a[M],b;
scanf("%d",n);?if?(?nM?)?n=M;
for?(?i=0;in;i++?)?scanf("%d",a[i]);
for?(?i=nn=1;in;i++?)?{
for?(?j=b=0;jnn;j++?)?if?(?a[i]==a[j]?)?{?b=1;break;?}
if?(?b==0?)?{?a[nn]=a[i];?nn++;?}
}
for?(?i=0;inn;i++?)?printf("%d?",a[i]);?printf("\n");
}
#include?"stdafx.h"
#include?string.h
#include?stdio.h
#include?stdlib.h
struct?STUDENT{
int?Number;
char?Name[32];
int??Class;
float?Score;
};
#define?IS_WHITE(ch)?((unsigned?char)ch=15?||?ch=='?')
//trim函數(shù)去除字符串str前后的空白字符
char?*trim(char?*str)
{
char?ch;
char?*p,?*q,?*r;
p=str;?q=str;?r=str-1;
while(1)
{
ch=*p;
if?(!IS_WHITE(ch))?break;
if?(ch==0)?break;
p++;
}
while(1)
{
ch=*p++;
*q=ch;
if?(ch==0)?break;
if?(!IS_WHITE(ch))?r=q;
q++;
}
*++r=0;
return?str;
}
int?InputStudent(const?char?*filename)
{
FILE?*f;
int?n;
char?str[200];
struct?STUDENT?student;
f=fopen(filename,"wb");
if?(f==NULL)?return?0;???//返回0表示失敗
for?(n=0;?;?++n)
{
printf("請輸入學號:?");?scanf("%d",?student.Number);
if?(student.Number==0)?break;
while(1)
{
printf("請輸入姓名:");??gets(str);
trim(str);
if?(str[0]==0)?continue;?//確保姓名不為空
strncpy(student.Name,?str,?sizeof(student.Name)-1);
//上句確保輸入姓名長度不超過31字節(jié)
break;
}
printf("請輸入班級:\n");?scanf("%d",?student.Class);
printf("請輸入成績:\n");?scanf("%f",?student.Score);
fwrite(student,?sizeof(student),1,?f);
//??????printf("%08d?%-32s?%-3d?%f\n",
//??????????student.Number,
//??????????student.Name,
//??????????student.Class,
//??????????student.Score);
}
fclose(f);
return?n;???//返回有多少個學生信息錄入
}
void?SortStudent(struct?STUDENT?*student,?int?n)
{
int?i,j;
struct?STUDENT?*a,?*b,?t;
for?(j=0;?jn-1;?++j)
{
a=student+j;
for?(i=j+1;?in;?++i)
{
b=student+i;
if?(a-Number=b-Number)?continue;?//否則交換
memcpy(t,a,sizeof(struct?STUDENT));
memcpy(a,b,sizeof(struct?STUDENT));
memcpy(b,t,sizeof(struct?STUDENT));
}
}
}
void?PrintStudent(const?struct?STUDENT?*student,?int?n)
{
int?i;
for?(i=0;?in;?++i,?++student)
printf("%08d?%-32s?%-3d?%6.1f\n",
student-Number,
student-Name,
student-Class,
student-Score);
}
void?main()
{
int?n1,?n2,?n;
struct?STUDENT?*student;
const?char?fn1[]="d:\\student1.bin";?//文件1
const?char?fn2[]="d:\\student2.bin";?//文件2
const?char?fn[]="d:\\student.bin";???//合并后的文件
FILE?*f;
printf("學生信息錄入(錄入過程中,學號輸入為0,則退出錄入)\n");
printf("第一步:%s中的學生信息錄入:\n",?fn1);
n1=InputStudent(fn1);
if?(0==n1)
{
printf("%s中的學生信息錄入有誤,退出程序\n",fn1);
return;
}
printf("第二步:%s中的學生信息錄入:\n",?fn2);
n2=InputStudent(fn2);
if?(0==n2)
{
printf("%s中的學生信息錄入有誤,退出程序\n",fn2);
return;
}
printf("第三步:合并%s,?%s\n",fn1,fn2);
student=(struct?STUDENT?*)malloc((n1+n2)*sizeof(struct?STUDENT));
f=fopen(fn1,"rb");
if?(f==NULL)?{printf("%s打開失敗,退出程序\n",fn1);?return;}
n=fread(student,sizeof(struct?STUDENT),?n1,?f);
fclose(f);
if?(n!=n1)?{printf("%s讀入錯誤,退出程序\n",fn1);?return;}
f=fopen(fn2,"rb");
if?(f==NULL)?{printf("%s打開失敗,退出程序\n",fn2);?return;}
n=fread(student+n1,sizeof(struct?STUDENT),?n2,?f);
//??PrintStudent(student,n1+n2);??//輸出學生信息
fclose(f);
if?(n!=n2)?{printf("%s讀入錯誤,退出程序\n",fn2);?return;}
n=n1+n2;
SortStudent(student,n);
f=fopen(fn,"wb");
if?(f==NULL)?{printf("%s建立失敗,退出程序\n",fn);?return;}
fwrite(student,?sizeof(struct?STUDENT),?n,?f);
fclose(f);
PrintStudent(student,n);??//輸出學生信息
free(student);
}