重复字符
要学会利用API,下面这道题用lastIndexOf()这个方法
/*
String s="abcdebxyz"
判断其中有没有重复的字符
*/
public class _重复字符 {
public static void main(String[] args) {
String s="abcdebxyz";
boolean flag =false;//false表示没有重复,先假设没有重复
for (int i = 0; i <s.length() ; i++) {
char c = s.charAt(i);//i表示索引
if (s.lastIndexOf(c)!=i){ //如果s字符串从后往前的数对应的索引没有对应上自身的i索引,说明有重复
flag=true; //true表示重复
break;//找到重复就可以终止
}
}
System.out.println(flag);
}
}
输出结果:true
串与字符的转换
/*
String s="swadawdas";
*/
然后是toCharArray()方法
public class _串与字符的转换 {
public static void main(String[] args) {
String s="swadawdas";
char c[] =s.toCharArray();//转换为字符数组
for (int i = 0 ; i <c.length ; i++) {
c[i]++;//加密
}
String s1=new String(c);//再把字符数组变成字符串
System.out.println(s1);
}
}
输出结果:txbebxebt
串与数值的转换
/*
String s = "456461"
*/
public class _串与数值的转换 {
public static void main(String[] args) {
String s="456461";
int n=0;
for (int i = 0; i <s.length() ; i++) {
n= n*10+(s.charAt(i)-'0');
}
System.out.println(n);
}
}
进制转换
/*
任意进制间的转换
n进制转m进制
String s = “2001201102”
3进制转换为5进制
先转换为10进制,再转换为5进制
*/
public class _进制转换01 {
public static void main(String[] args) {
String s="2001201102";//三进制的串
int n =0;
for (int i = 0; i <s.length() ; i++) {
char c= s.charAt(i);
// n=n*10+(c-'0');
n=n*3+(c-'0');//把三进制的串转为十进制串,以一当三
}
System.out.println("十进制的串:"+n);
String s2="";
while (true){
s2=n%5 + s2;//把十进制的串转为五进制
n =n/5;//舍去这个位
if(n==0) break;
}
System.out.println("五进制的串:"+s2);
}
}