1、模拟一个trim()方法,去除字符串两端的空格。
方法一:自己写的,较复杂
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s1 =new String(" abcde ");
String s2="abc";
for(int i=0;i<s1.length();i++){
if(s1.charAt(i)!=' '){
s1=s1.substring(i);
i=i+s1.length();
}
}
for(int i=0;i<s1.length();i++){
if(s1.charAt(i)==' '){
s2=s1.substring(0, i)+s2;
i=i+s1.length();
}
}
System.out.println(s2);
}
}
结果为:abcdeabc
方法二:
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s1 =new String(" abcde ");
String s2="abc";
int x=0;
int y=s1.length()-1;
while(s1.charAt(x)==' '){
x++;
}
while(s1.charAt(y)==' '){
y--;
}
s2=s1.substring(x,y+1)+s2;
System.out.println(s2);
}
}
结果为:abcdeabc
通用方法式:
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s1=" ddd ";
sop("["+s1+"]");
sop("["+mytrim(s1)+"]");
}
public static void sop(String str){
System.out.println(str);
}
public static String mytrim(String str){
int start=0;
int end=str.length()-1;
while(start<end & str.charAt(start)==' '){
start++;
}
while(str.charAt(end)==' '){
end--;
}
return str.substring(start, end+1);
}
}
结果为:
2、将字符串反转
思路:将字符串变数组,读数组反转,将数组变成字符串。
法一:
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s1="abcde";
System.out.println(s1);
char[] c1=s1.toCharArray();
char[] c2= new char[c1.length];
for(int i=0;i<c1.length;i++){
c2[c1.length-i-1]=c1[i];
}
s1=String.copyValueOf(c2);
System.out.println(s1);
}
}
结果为:
abcde
edcba
法二:分成小模块写。
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s="abcde";
System.out.println(s);
System.out.println(StringReverse(s));
}
public static String StringReverse(String str){
char[] c =str.toCharArray();
Reverse(c);
return new String(c);
}
private static void Reverse(char[] c) {
for(int start=0,end=c.length-1;start<end;start++,end--){
swap(c,start,end);
}
}
private static void swap(char[] c, int start, int end) {
char temp =c[start];
c[start]=c[end];
c[end]= temp;
}
}
结果为:
abcde
edcba
写法3:
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s="abcdefghi";
System.out.println(s);
System.out.println(StringReverse(s));
}
public static String StringReverse(String str){
char[] c =str.toCharArray();
return Reverse(c,0,str.length());
}
private static String Reverse(char[] c,int x, int y) {
for(int start =x,end=y-1;start<end;start++,end--){
swap(c,start,end);
}
return new String(c);
}
private static void swap(char[] c, int start, int end) {
char temp =c[start];
c[start]=c[end];
c[end]= temp;
}
}
结果为:
abcdefghi
ihgfedcba
3、将字符串中指定部分进行反转。
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s="abcdefghi";
System.out.println(s);
System.out.println(StringReverse(s,1,8));
}
public static String StringReverse(String str,int x,int y){
char[] c =str.toCharArray();
Reverse(c,x,y);
return new String(c);
}
private static void Reverse(char[] c,int x, int y) {
for(int start =x,end=y-1;start<end;start++,end--){
swap(c,start,end);
}
}
private static void swap(char[] c, int start, int end) {
char temp =c[start];
c[start]=c[end];
c[end]= temp;
}
}
结果为:
abcdefghi
ahgfedcbi
4、一个字符串中某个字母出现的次数。
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s="abdkkkdejkik";
System.out.println("字符串中k字母出现的次数为: "+counter1(s,'k'));
}
public static int counter1(String s,char c){
int num =0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)==c){
num++;
}
}
return num;
}
}
结果为:
字符串中k字母出现的次数为: 5
5、获取一个字符串在另一个字符串中出现的次数。
法一:
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s="abdkkdejkk";
System.out.println("字符串中kk字符串出现的次数为: "+counter1(s,"kk"));
}
public static int counter1(String s,String c){
int num =0;
int x=c.length();
int y=0;
while(s.contains(c)){
num++;
y=s.indexOf(c);
s=s.substring(y+x);
}
return num;
}
}
结果为:
字符串中kk字符串出现的次数为: 2
法二:
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s="abdkkdejkk";
System.out.println("字符串中kk字符串出现的次数为: "+counter1(s,"kk"));
}
public static int counter1(String s,String c){
int num =0;
int x=c.length();
int i=0;
while((i=s.indexOf(c,i))!=-1 ){
num++;
i=i+x;
}
return num;
}
}
结果为:
字符串中kk字符串出现的次数为: 2
注意看一下代码,可以看看输出结果:
package j2se.String;
public class TestStringDemo1 {
public static void main(String[] args) {
String s="kkabdkkdej";
System.out.print("字符串中kk出现的次数为: ");
System.out.print(s.split("kk").length);
}
public static int counter1(String s,String c){
int num =0;
int x=c.length();
int i=0;
while((i=s.indexOf(c,i))!=-1 ){
num++;
i=i+x;
}
return num;
}
}
结果为: 字符串中kk出现的次数为: 3
我什么会这样呢?请读者思考......
6、获取两个字符串中最大相同子串。
1)
package j2se.String;
public class TestStringDemo2 {
public static void main(String[] args) {
String s1 ="abchellonihao";
String s2="hellod";
int x2=s2.length();
int y=x2;
boolean flag=true;
while(flag){
for(int i=0;i+y<x2;i++){
if(s1.contains(s2.substring(i,i+y))){
System.out.println("最大相同子串: "+s2.substring(i,i+y));
flag=false;
i=i+x2;
}
y--;
}
}
}
}
结果为:
最大相同子串: hello
2)
package j2se.String;
public class TestStringDemo3 {
public static void main(String[] args) {
String st1="abchellonihao";
String st2="hellod";
find_sub(st1,st2);
}
public static void find_sub(String s1,String s2){
String ss1,ss2;
int x1=s1.length();
int x2=s2.length();
if(x1>x2){
ss1=s1;
ss2=s2;
}else{
ss1=s2;
ss2=s1;
}
int y=ss2.length();
boolean flag=true;
while(flag){
for(int i=0;i+y<ss2.length();i++){
if(ss1.contains(ss2.substring(i,i+y))){
System.out.println("最大相同子串: "+ss2.substring(i,i+y));
flag=false;
i=i+ss2.length();
}
y--;
}
}
}
}
结果为:
最大相同子串: hello
3)
package j2se.String;
public class TestStringDemo3 {
public static void main(String[] args) {
String s2="afddehellodel";
String s1="heldloddd";
sop(getMaxSubString(s1, s2));
}
public static void sop(String str){
System.out.println(str);
}
public static String getMaxSubString(String s1,String s2){
String max=" ",min=" ";
max=(s1.length()>s2.length())?s1:s2;
min=(max==s1)?s2:s1;
for(int x=0;x<min.length();x++){
for(int y=0,z=min.length()-x;z!=min.length()+1;y++,z++){
String temp=min.substring(y,z);
if(max.contains(temp)) //if(s1.indexOf(s2)!=-1)
{
return temp;
}
}
}
return " ";
}
}