06-字符串

06-字符串

属性 截取字符串的长度str.length
方法:
charAt() 方法可返回指定位置的字符
concat() 方法用于连接两个或多个字符串
indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。区分大小写
//indexOf(查找的值,开始的位置)
lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置
includes() 方法用于判断字符串是否包含指定的子字符串 返回布尔型 true false
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串
//replace(searchValue,newValue) 返回的是新的字符串
split() 方法用于把一个字符串分割成字符串数组 见js文件
substr() 方法可在字符串中抽取从开始下标开始的指定数目的字符
//substr(start,length)
substring() 方法用于提取字符串中介于两个指定下标之间的字符
//substring(from,to)
slice(start, end) 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分
substring和slice的区别见js文件
toLowerCase() 方法用于把字符串转换为小写
toUpperCase() 方法用于把字符串转换为大写
trim() 方法用于删除字符串的头尾空格

js文件:

var str = 'hello wrold';
var str1 = 'monkey ';
//属性   截取字符串的长度
	document.write(str.length);   //11
//charAt() 方法可返回指定位置的字符
	document.write(str.charAt(1));   //e
	document.write(str.charAt(str.length-1));   //d 获取最后一个字符
//concat() 方法用于连接两个或多个字符串
	var s = str1.concat(str,' welcome'); //monkey hello world welcome
//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。区分大小写	
	document.write(str.indexOf('o'));   //4  匹配成功后返回索引值
	document.write(str.indexOf('a'));   //-1 没有匹配成功则返回-1
	document.write(str.indexOf('o',5));   //8  indexOf(查找的值,开始的位置)
//lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置
	document.write(str.lastIndexOf('o'));   //8	
	document.write(str.lastIndexOf('o',5));   //4
//replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串
//replace(searchValue,newValue)   返回的是新的字符串
	var s = str.replace('hello','hi,'); //hi,world
//split() 方法用于把一个字符串分割成字符串数组
	var str3 = 'how,are,you';
	document.write(str3.split(","));   // ["how", "are", "you"]
	document.write(str3.split(",",2));   // ["how", "are"]   2表示返回数组的最大长度
//substr() 方法可在字符串中抽取从开始下标开始的指定数目的字符
	document.write(str.substr(4)); //o wrold
	document.write(str.substr(2,4));//substr(start,length)   "llo "
//substring() 方法用于提取字符串中介于两个指定下标之间的字符	 
	document.write(str.substring(4));  //o wrold
	document.write(str.substring(2,4));  //substr(from,to)   ll  不包括to
//slice(start, end) 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分
	document.write(str.slice(2,4)); //ll
	document.write(str.slice(-1)); //d  -1表示最后一个字符串
	document.write(str.substring(-1));   //-1   表示0 hello world
//slice()和substring()区别  思考题
/* var str="abcdefghijkl";
	console.log(str.slice(3,-4));  //defgh
	console.log(str.substring(3,-4));  //abc*/	
//toLowerCase() 方法用于把字符串转换为小写
//toUpperCase() 方法用于把字符串转换为大写	
//trim() 方法用于删除字符串的头尾空格
	var str5 = '    hello    ';
	document.write(str5.trim());	//hello	
在Java中,我们可以使用SimpleDateFormat类来将时间字符串转换为年月日格式。首先,你需要创建一个SimpleDateFormat的对象,并指定日期格式,例如"yyyy-MM-dd"表示年月日的格式。然后,使用该对象的parse()方法将时间字符串解析为一个Date对象。最后,再次使用SimpleDateFormat对象的format()方法将Date对象格式化为另一种日期格式,例如"yyyy年MM月dd日"表示年月日的格式。 以下是一个示例代码: ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateConversion { public static void main(String[] args) { String dateString = "2021-06-10"; SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { Date date = dateFormat.parse(dateString); SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy年MM月dd日"); String formattedDate = outputFormat.format(date); System.out.println(formattedDate); } catch (ParseException e) { e.printStackTrace(); } } } ``` 在上面的代码中,我们首先创建了一个SimpleDateFormat对象dateFormat,用于解析时间字符串"2021-06-10"。然后使用parse()方法将时间字符串解析为一个Date对象date。接下来,我们再次创建了一个SimpleDateFormat对象outputFormat,用于定义输出的日期格式为"yyyy年MM月dd日"。最后,使用format()方法将Date对象date格式化为所需的日期格式,并将结果打印出来。 运行以上代码,将输出"2021年06月10日"。这样,我们就成功地将时间字符串转换为年月日格式。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值