C函数仿写 JAVA::String操作

本文详细介绍了Java中用于处理字符串的HLUtil库中的各种函数,包括替换、截取、查找等操作,帮助开发者更高效地进行字符串处理。

模拟JAVA String 函数处理

HLUtil.h

 

/**

* Ext-C Java::String.

* replaceFirst Str1 string will replace the first occurrence of the string str2 into str3.

*/

static void replaceFirst(char *str1,char *str2,char *str3);

 

 

/**

* Ext-C Java::String.

* Will appear in all str2 str1 are replaced str3.

*/

        static void replace(char *str1,char *str2,char *str3);

 

 

/**

* Ext-C Java::String.

* Src string interception, labeled start from the beginning to the end-1 (end front) of the string stored in dest (index starts at 0). 

*/

static void substring(char *dest,char *src,int start,int end);

 

 

/**

* Ext-C Java::String.

* Return the src subscript index character.

*/

static char charAt(char *src,int index);

 

 

/**

* Ext-C Java::String.

* Return the position of the first occurrence of str2 (The following table index) in str1, there is no return -1.

*/

static int indexOf(char *str1,char *str2);

 

 

/**

* Ext-C Java::String.

* Location (subscript) returns the last occurrence of str1 str2, there is no return -1.

*/

static int lastIndexOf(char *str1,char *str2);

 

 

/**

* Ext-C Java::String.

* Remove the first non-blank character in front of the left str whitespace characters (spaces and horizontal tabs).

*/

static void ltrim(char *str);

 

 

/**

* Ext-C Java::String.

* Delete str last non-blank character behind all whitespace characters (spaces and horizontal tabs).

*/

static void rtrim(char *str);

 

 

/**

* Ext-C Java::String.

* Whitespace characters to delete str ends.

*/

static void trim(char *str);

 

 

HLUtil.cpp:

void HLUtil:: replaceFirst(char *str1,char *str2,char *str3)

{

int length = strlen(str1)+1;

if(length>1)

{

char *str4 =new char[length];

char *p;  

strcpy(str4,str1);  

if((p=strstr(str1,str2))!=NULL)

{  

while(str1!=p&&str1!=NULL)

{  

str1++;  

}  

str1[0]='\0'; 

strcat(str1,str3);

strcat(str1,strstr(str4,str2)+strlen(str2));

}

delete str4;

str4 = NULL;

}

}

 

 

void HLUtil:: replace(char *str1,char *str2,char *str3)

{

while(strstr(str1,str2)!=NULL)  

{  

replaceFirst(str1,str2,str3);  

}

}

 

 

void HLUtil:: substring(char *dest,char *src,int start,int end)

{

int i=start;  

if(start>strlen(src))return;  

if(end>strlen(src))  

end=strlen(src);  

while(i<end)  

{     

dest[i-start]=src[i];  

i++;  

}  

dest[i-start]='\0';  

return;

}

 

 

char HLUtil:: charAt(char *src,int index)

{

char *p=src;  

int i=0;  

if(index<0||index>strlen(src))  

return 0;  

while(i<index)i++;  

return p[i];

}

 

 

int HLUtil:: indexOf(char *str1,char *str2)

{

char *p=str1;  

int i=0;  

p=strstr(str1,str2);  

if(p==NULL)  

return -1;  

else{  

while(str1!=p)  

{  

str1++;  

i++;  

}  

}  

return i;  

}

 

 

int HLUtil:: lastIndexOf(char *str1,char *str2)

{

char *p=str1;  

int i=0,len=strlen(str2);  

p=strstr(str1,str2);  

if(p==NULL)return -1;  

while(p!=NULL)  

{  

for(;str1!=p;str1++)i++;  

p=p+len;  

p=strstr(p,str2);  

}  

return i;

}

 

 

void HLUtil:: ltrim(char *str)

{

int i=0,j,len=strlen(str);  

while(str[i]!='\0')  

{  

if(str[i]!=32&&str[i]!=9)break;

i++;  

}  

if(i!=0)  

for(j=0;j<=len-i;j++)  

{     

str[j]=str[j+i];

}

}

 

 

void  HLUtil:: rtrim(char *str)

{

char *p=str;  

int i=strlen(str)-1;  

while(i>=0)  

{  

if(p[i]!=32&&p[i]!=9)break;  

i--;  

}  

str[++i]='\0';

}

 

 

void HLUtil:: trim(char *str)

{

ltrim(str);  

rtrim(str);

}

 

定义并制作一个简易的学生信息管理系统,具有数据的添加、查询和排序的功能; 要求使用Record类型(相当于Java的Map),在定义Record的字段时,使用泛型; 设计思路: (1)定义学生类,含有姓名、年龄、专业、寝室号等,并且定义学生信息输出方法; (2)在Record容器中,以学生的姓名为key,其他信息实体为Value, (3)在程序中,定义数据管理类,首先通过代码初始化一批数据,并定义添加方法、查询方法、排序方法; (4)在页面中,通过按钮点击调用数据管理类,如果能够将数据的输入、输出放在页面上实现,那就更好了/** * 该文件中定义了学生类,学生管理类,实现学生信息的初始化、增加、排序等功能; * */ //学生信息类 export class Student{ name:string=''; //姓名 age:number=0; //年龄 height:number=0; // 身高 prof:string=''; //专业 constructor(name:string, age:number, height:number, prof:string) { this.name = name; this.age = age; this.height = height; this.prof = prof; } toString():string{ return this.name+', age:'+this.age+",height"+this.height+",prof:"+this.prof; } } //学生信息管理类 export class StudentMgr{ stus:Record<string,Student> ={ 'wang': new Student('wang',22,185,'计算机'), 'zhao': new Student('zhao',21,170,'物联网'), 'liu': new Student('liu',20,175,'人工智能'), 'xiao': new Student('xiao',23,174,'计算机') } //构造方法,再添加一个数据 constructor() { this.stus['pan']= new Student('pan',20,165,'物联网'); } //添加一个学生,返回添加的结果 addStudent(name:string, age:number, height:number,prof:string):string{ let stu:Student = this.query(name); if(stu===undefined || stu===null) { this.stus[name] = new Student(name, age, height, prof); return '新学生添加成功!'; }else{ this.stus[name] = new Student(name, age, height, prof); return '该学生已经存在,信息更新成功!'; } } //根据key值,查询一个学生 query(name:string):Student{ return this.stus[name]; } //对所有的学生进行排序; sort(){ // 将 stus 对象转换为数组并按照 age 排序 let sortedStudents = Object.values(this.stus).sort( (a, b) => { return a.height - b.height ;} ); //Comparator compareTo(o1,o2) // 输出排序后的结果 sortedStudents.forEach(student => { console.log(student.toString()); }); } 根据代码仿与要求有关的鸿蒙OSapp
最新发布
03-19
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值