------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
class StringMethodDemo
{
public static void method_get()
{
String str = "Zhangsa,lisi,wangwu";
String[] arr = s.split(",");
for (int x=0;x<arr.length;x++)
{
sop(arr[x]);
}
}
public static void method_replace()
{
String s = "hello java";
String s1 = s.replace("java","world");
sop("s="+s);
sop("s1="+s1);
}
public static void method_trans()
{
char[] arr = {'a','b','c','d','e','f'};
String s = new String(arr,1,3);
sop("s="+s);
String s1 = "zxcvbnm";
char[] chs = s1.toCharArray();
for(int x=0;x<chs.length;x++)
{
sop("ch="+chs[x]);
}
}
public static void method_is()
{
String str = "ArrayDemo.java"
sop(str.startsWith("Array"));
sop(str.endsWith(".java"));
sop(Str.contains(".java"));
}
public static void method_get()
{
String str = "abcdeakpf";
sop(str.length());
sop(str.charAt(4));
sop(str.indexOf('m',3));
sop(str.lastIndexOf("a"));
}
public static void main(String[] args)
{
method_split()
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
//练习一,去除字符串两端空格。
class StringTest
{
public static void sop(String str)
{
System.out.println(str);
}
public static void main(String[] args)
{
String s= " ab cd ";
sop("("+s+")")
s = myTrim(s);
sop("("+s+")")
}
public static String myTrim(String str)
{
int start = 0,end =str.length()-1;
while(Strart<=end && str.charAt(start)==' ')
start++;
while(Strart<=end && str.charAt(end)==' '
end--;
return str.substring(start,end+1);
}
}
while(S.charAt(pos)=='-')
pos++;
int x = s.length()-1;
while(s.charAt(x)=='')
x--;
//练习二:将字符串反转。
思路:
1 将字符串变成数组
2 对数组反转
3 将数组变成字符串
public static String reverseString(String s)
{
//字符变数组
char[] chs = s.toCharArray();
// 反转数组。
reverse(chs);
//数组变成字符串
return new String
}
private static void reverse(char[] arr)
{
for(int start=0,end=arr.length-1;start<end;start++,end--)
{
swap(arr,start,end);
}
}
private static void swap(char[] arr,int x,int y)
{
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
//练习三 获取一个字符串在另一个字符串中出现的次数。
public static int getSubCount(String str,String key)
{
int count = 0;
int index = 0;
while(str.indexOf(key))!=-1)
{
sop("str="+str);
strSubString(index+key.length());
count++;
}
return count;
}
public static void main(String[] args)
{
String str = "abkkcdkkefkkskk";
sop("count="+getSubCount(str,"kk"));
//sop("count="+getSubCount_2(str,"kk"));
}
public static void sop(String str)
{
System.out.println(str);
}
}
//练习四 获取两个字符串中最大相同数的整数 思路:1 将短的那个子串按照长度递减的方式获取到;2将每获取到的子串去长串中判断是否包含,如包含,找到!
public static String getMaxSubString(String s1,String s2)
{
/*String max = "",min = "";
max = s1.length()>s2.length())?s1:s2;
min = (max==s1)?s2:s1
sop("max="+max+"...min"+min)*/
for(int x=0;x<s2.length();x++)
{
for (int y=0;z=s2.length()-x;z!=s2.length()+1;y++,z++)
{
String temp = s2.substring(y,z);
//sop(temp);
if (s1.contains(temp))//if(s1.indexOf(temp)!=-1
return temp;
}
}
return"";
}
public static void main(String[] args)
{
String s1 = "abcwerthelloyuiodef";
String s2 = "cvhellobnm";
sop(getMaxSubString(s1,s2));
}
public static void sop(String str)
{
System.out.println(str);
}
}