<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script type="text/javascript">
var str='abc234fedgoodboy';
var str1="hello";
var str2=" 1234 ";
var str3='1,2,3,4';
console.log(str);
//从下标截取长度为5的字符串
console.log(str.substr(4,5));
//截取出4-8下标的字符串
console.log(str.slice(4,9));
console.log(str.substring(4,9));
//查找索引为8的字符及字符编码
console.log(str.charAt(8));
console.log(str.charCodeAt(8));
//连接字符串
console.log(str.concat(str1));
//去除字符前后的空格
console.log(str2.trim());
//小写转换为大写
var up=str.toUpperCase();
console.log(up);
//大写转小写
var lower=up.toLowerCase();
console.log(lower);
//字符4的字符替换为y---未改变原字符串
str.replace('4','y');
console.log(str);
console.log(str.replace('4','y'));
//将字符串以逗号分割为数组 ---未改变原字符串
str3.split(',');
console.log(str3);
console.log(str3.split(','));
//字符串去重
//var str='abc234fedgoodboy';
//indexOf(value,startIndex)-- 找到值为value的索引
var newSrt='a';
for(var i=1;i<str.length-1;i++){
for(var j=0;j<newSrt.length;j++){
if(newSrt[j]!==str[i]){
if(j===newSrt.length-1){
newSrt=newSrt.concat(str[i]);
}
}else{
break;
}
}
}
console.log(newSrt);
//字符串去重
//var str='abc234fedgoodboy';
//indexOf(value,startIndex)-- 找到值为value的索引
var newSrt='a';
for(var i=1;i<str.length-1;i++){
for(var j=0;j<newSrt.length;j++){
if(newSrt.indexOf(str[i])===-1){
newSrt=newSrt.concat(str[i]);
}
}
}
console.log(newSrt);
//找到串中重复的值放到新串中
//var str='abc234fedgoodboy';
var newStr1='';
for(var i=0;i<str.length-1;i++){
if(newStr1.indexOf(str[i])===-1&&str.indexOf(str[i],i+1)>=i+1){
newStr1=newStr1.concat(str[i]);
}
}
console.log(newStr1);
</script>
</body>
</html>