网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  string类使用的例子(3)     
  文章作者:未知  文章来源:水木森林  
  查看:85次  录入:管理员--2007-11-17  
 
  if ("first"==strfl)
console.writeline("the index value returned is : "+ objstring.str.indexofany(c,intstart));

else
console.writeline("the index value returned is : "+ objstring.str.lastindexofany(c,intstart));

break;
case 3:
console.write("enter the string for the character array :");
strchar=console.readline();
c=strchar.tochararray();
console.write("enter the starting index for search :");
intstart=int.parse(console.readline());
console.write("enter the number of characters to searc :");
intcount=int.parse(console.readline());
if ("first"==strfl)
console.writeline("the index value returned is : "+ objstring.str.indexofany(c,intstart,intcount));

else
console.writeline("the index value returned is : "+ objstring.str.lastindexofany(c,intstart,intcount));

break;
case 4:
blnstay=false;
break;
}
if (blnstay)
mtdindexanyimpl(strvalue,strfl);
}

private void mtdinsert() {
console.writeline("string.insert(int index,string str) - > this functions returns the original string with ‘str‘ inserted at ‘index‘");

console.writeline("the original string : " + objstring.str);
console.write("enter the string to be inserted : ");
string strtmp=console.readline();
console.write("enter the position where it has to be inserted :");
int inttmp=int.parse(console.readline());
objstring.str=objstring.str.insert(inttmp,strtmp);
console.writeline("the modified string : " + objstring.str);
}

private void mtdjoin() {
string[] s=;
console.writeline("1.string.join(string str,string[] strarr) - > this functions joins the string arrays using ‘str‘");

console.writeline("2.string.join(string str,string[] strarr,int i,int j) - > this functions joins the string arrays using ‘str‘ starting from the ‘i‘ th array element and ‘j‘ number of elements after it. ");

console.write("enter your choice :");
string strchoice=console.readline();
if ("1"==strchoice) {
console.writeline("the string array is :str[0]=‘welcome‘,str[1]=‘to‘,str[2]=‘the‘,str[3]=‘world‘,str[4]=‘of‘,str[5]=‘c#‘");

console.write("enter the string with which to join : ");
string strtmp=console.readline();
console.writeline("the joint string is : " + string.join(strtmp,s));
}
else {
console.writeline("the string array is :str[0]=‘welcome‘,str[1]=‘to‘,str[2]=‘the‘,str[3]=‘world‘,str[4]=‘of‘,str[5]=‘c#‘");

console.write("enter the string with which to join : ");
string strtmp=console.readline();
console.write("enter the starting index of the array : ");
int intstart=int.parse(console.readline());
console.write("enter the number of elements to join :" );
int intcount=int.parse(console.readline());
console.writeline("the joint string is : " + string.join(strtmp,s,intstart,intcount));

}
}

private void mtdlastindex() {
console.writeline("string.lastindexof() - > this returns the index of the last occurence of a charcter or string in the given string.");

console.writeline("the search of the string stops when the required value is founds or proceedes until the beginning of the string has been reached");

console.writeline("it returns the index if the value is found or ‘-1‘ if not found.");
mtdindeximpl("lastindex","last");

}

private void mtdlastindexany() {
console.writeline("string.lastindexofany() - > this returns the index of the last occurence of any charcter of the character array in the given string.");

console.writeline("the search of the string stops when the required value is founds or proceedes until the beginning of the string has been reached");

console.writeline("it returns the index if the value is found or ‘-1‘ if not found.");
mtdindexanyimpl("lastindex","last");
}

private void mtdlength() {
console.writeline("string.length - > this property returns the length of the string.");
console.writeline("the length of ‘"+objstring.str+"‘ is : "+objstring.str.length);
}

private void mtdpadleft() {
mtdpad("left");
}

private void mtdpad(string strval) {
console.writeline("string.pad"+strval+"() - > this method pads spaces or some other character to the "+strval+" of the string");

console.writeline("string.pad"+strval+"(int i) -> fills spaces to the "+strval+" of the string, ‘i‘ specifies the length of the string along with spaces");

console.writeline("string.pad"+strval+"(int i,char c) -> fills the character ‘c‘ to the "+strval+" of the string, ‘i‘ specifies the length of the string along with spaces");

console.writeline("the original string :"+objstring.str );
console.write("enter the length of the desired string : ");
int intstart=int.parse(console.readline());
console.write("enter the character to be padded(enter nothing for spaces) :");
string strtmp=console.readline();
if(!strtmp.equals("")) {
char c=(strtmp.tochararray())[0];
if ("left"==strval)
console.writeline("the padded string : " + objstring.str.padleft(intstart,c));
else
console.writeline("the padded string : " + objstring.str.padright(intstart,c));
}
else
if ("left"==strval)
console.writeline("the padded string : " + objstring.str.padleft(intstart));
else
console.writeline("the padded string : " + objstring.str.padright(intstart));

}

private void mtdpadright() {
mtdpad("right");
}

private void mtdremove() {
console.writeline("string.remove(int i,int j) - > removes a part of the string.‘i‘ represents the start position and ‘j‘ represents the length of string to be removed.");

console.writeline("the original string : "+objstring.str);
console.write("enter the starting position : ");
int intstart=int.parse(console.readline());
console.write("enter the length of string to be removed :");
int intlength=int.parse(console.readline());
console.writeline("the string after removal :"+objstring.str.remove(intstart,intlength));
}

private void mtdreplace() {
console.writeline("string.replace() - > replaces a character with another character or a string with another string throughout the given string");

console.writeline("1. string.replace(char cold,char cnew) -> replaces all occurances ‘cold‘ with ‘cnew‘");

console.writeline("2. string.replace(string sold,strin snew) -> replaces all occurances of ‘sold‘ with ‘snew‘");

console.write("enter your choice :");
int intchoice=int.parse(console.readline());
console.writeline("the original string is :"+objstring.str);
if (1==intchoice) {
console.write("enter the character to be replaced :");
char cold=(console.readline().tochararray())[0];
console.write("enter the new character :");
char cnew=(console.readline().tochararray())[0];
console.writeline("the string after replacing : "+objstring.str.replace(cold,cnew));
}
else {
console.write("enter the string to be replaced :");
string sold=console.readline();
console.write("enter the new string :");
string snew=console.readline();
console.writeline("the string after replacing : "+objstring.str.replace(sold,snew));

}
}

private void mtdsplit() {
console.writeline("this will be done later.");
}

private void mtdstartswith() {
console.writeline("string.startswith(string str) - > returns a boolean value indicating whether the string starts with ‘str‘");

console.writeline("the original string : "+ objstring.str);
console.write("enter the string to search for :");
string strtmp=console.readline();
if (objstring.str.startswith(strtmp))
console.writeline("the string ‘"+objstring.str+"‘ starts with ‘"+strtmp+"‘.");
else
console.writeline("the string ‘"+objstring.str+"‘ does not starts with ‘"+strtmp+"‘.");
}

private void mtdsubstr() {
console.writeline("string.substring() - > retrieves a part of the string from the original string");
console.writeline("1. string.substring(int i) -> retrieves the string starting from ‘i‘(zero based)");
console.writeline("2. string.substring(int i,int j) -> retrieves the string starting from ‘i‘ and having a length ‘j‘.");

console.write("enter your choice :");
int intchoice=int.parse(console.readline());
int intstart,intlength;
console.writeline("the original string :"+objstring.str);
if (1==intchoice) {
console.write("enter the position from where the substring should start :");
intstart=int.parse(console.readline());
console.writeline("the retrieved substring is :"+objstring.str.substring(intstart));
}
else {
console.write("enter the position from where the substring should start :");
intstart=int.parse(console.readline());
console.write("enter the length of the substring:");
intlength=int.parse(console.readline());
console.writeline("the retrieved substring is :"+objstring.str.substring(intstart,intlength));
}
}

private void mtdlower() {
console.writeline("string.tolower() - > returns the string with all its characters in lower case");
console.writeline("the original string : " + objstring.str);
console.writeline("the string in lower case : " +objstring.str.tolower());
}

private void mtdupper() {
console.writeline("string.toupper() - > returns the string with all its characters in upper case");
console.writeline("the original string : " + objstring.str);
console.writeline("the string in upper case : " +objstring.str.toupper());
}

private void mtdtrim() {
console.writeline("string.trim() - > removes white space characters from the begininning and end of the string and also specified characters.");

console.writeline("1. string.trim() -> removes white space characters from beginning and end of the string.");

console.writeline("2. string.trim(char[] c) -> removes all occurances of set of characters in the array from the beginning and end of string.");

console.write("enter your choice :");
int intchoice=int.parse(console.readline());
console.writeline("the original string : " +objstring.str);
if (1==intchoice) {
console.writeline("the trimmed string is : "+objstring.str.trim());
}
else {
console.write("enter the character array : ");
char[] c=console.readline().tochararray();
console.writeline("the string after removing characters from the array : " + objstring.str.trim(c));

}
}

private void mtdtrimend() {
console.writeline("string.trimend(char[] c) - > removes all occurances of the set of characters in the array from the end of the string.");

console.writeline("the original string is : " + objstring.str);
console.write("enter the character array : ");
char[] c=console.readline().tochararray();
console.writeline("the modified string is : "+objstring.str.trimend(c));
}

private void mtdtrimstart() {
console.writeline("string.trimstart(char[] c) - > removes all occurances of the set of characters in the array from the start of the string.");

console.writeline("the original string is : " + objstring.str);
console.write("enter the character array : ");
char[] c=console.readline().tochararray();
console.writeline("the modified string is : "+objstring.str.trimstart(c));
}
}

 
 
上一篇: java语言深入 多线程程序模型研究    下一篇: 用ant构造application
  相关文档
开源技术:如何在eclipse中构建备忘单 11-16
why java can be used for games? 11-17
基于jcom搭建java-微软信息桥梁 11-17
java web 服务学习报告—web简介 11-17
java基础问题请教! 11-17
rms 从入门到精通౿.. 11-17
在java applet中如何显示另外一个html页面? 11-17
togmtstring 方法 11-16
j2ee设计模式浅谈(1) 11-17
is it a problem on regex or static 11-17
使用ejb 3.0简化企业级java开发二 11-17
多线程从线程继承 11-17
用消息交换增强ebxml的安全性 11-17
ejb技术之旅(三) 11-16
开发框架:利用struts实现国际化支持 12-25
palm j2me串行通讯程序编写与调试 11-17
java版本和c++版本简单stack程序 11-17
cell插件在j2ee系统中的应用 11-17
使用decorator模式实现日期选择组件(4) 11-17
诊断java代码:设计轻松的代码维护 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息