下面是关于字符串的一些操作,这些都是在一本C++的书上看到的,然后就用java实现了一下。
public class StringOperation
{
public static void main(String[] args)
{
String srcstr = "abcdefg";
String substr = "cdef";
String str = "I am from shandong";
String token = "from";
String isrevstr = "abcdcba";
String str1 = "abcdefg aeiou";
String str2 = "acdefgae";
String str3 = "abccdefcfgcsd";
String str4 = "123456789";
String str5 = "bei北京欢迎你jing我爱你中国China";
System.out.println(Search(srcstr, substr));
System.out.println(Reverse(str));
System.out.println(Reverse2(str));
System.out.println(Reverse(str, token));
System.out.println(isRevStr(isrevstr));
System.out.println(Compare(srcstr, substr));
System.out.println(getCommonStr(str1, str2));
System.out.println(DeleteStr(str3, 3, 3));
System.out.println(DeleteStr(str3, 'c'));
System.out.println(RightMove(str4, 6));
System.out.println(LeftMove(str4, 6));
System.out.println(getChineseNum(str5));
}
public static int Search(String srcstr, String substr)
{
int index = -1;
index = srcstr.indexOf(substr);
return index;
}
public static String Reverse(String str)
{
String[] temp = str.split(" ");
StringBuffer sb = new StringBuffer();
for(int i=temp.length-1; i>=0; i--)
sb.append(temp[i] + " ");
return sb.toString();
}
public static String Reverse2(String str)
{
StringBuffer sb = new StringBuffer();
sb.append(str);
return sb.reverse().toString();
}
public static String Reverse(String str, String token)
{
StringBuffer sb = new StringBuffer();
StringBuffer temp = new StringBuffer();
sb.append(str.replaceFirst(token, temp.append(token).reverse().toString()));
return sb.reverse().toString();
}
public static boolean isRevStr(String str)
{
boolean flag = true;
for(int i=0; i<str.length()/2; i++)
{
if(str.charAt(i) != str.charAt(str.length()-1-i))
{
flag = false;
break;
}
}
return flag;
}
public static int Compare(String str1, String str2)
{
int index = 0;
int len = 0;
if(str1.length() <= str2.length())
{
len = str1.length();
}
else
{
len = str2.length();
}
int i=0;
for(i=0; i<len; i++)
{
if(str1.charAt(i) == str2.charAt(i))
{
index = 0;
}
else if(str1.charAt(i) < str2.charAt(i))
{
index = -1;
break;
}
else
{
index = 1;
break;
}
}
if(i == len)
{
if(str1.length() < str2.length())
index = -1;
else if(i < str1.length())
index = 1;
}
return index;
}
public static String getCommonStr(String str1, String str2)
{
String temp = "";
String longstr = "";
String shortstr = "";
String commonstr = "";
boolean flag = false;
if(str1.length() >= str2.length())
{
longstr = str1;
shortstr = str2;
}
else
{
longstr = str2;
shortstr = str1;
}
for(int i=shortstr.length()-1; i>0; i--)
{
for(int j=0; j<shortstr.length()-i; j++)
{
temp = shortstr.substring(j, i+j+1);
if(longstr.indexOf(temp) >= 0)
{
flag = true;
commonstr = temp;
break;
}
}
if(flag)
break;
}
return commonstr;
}
public static String RightMove(String str, int length)
{
int len = length % str.length();
StringBuffer sb = new StringBuffer();
sb.append(str.substring(str.length()-len)).append(str.subSequence(0, str.length()-len));
return sb.toString();
}
public static String LeftMove(String str, int length)
{
int len = length % str.length();
StringBuffer sb = new StringBuffer();
sb.append(str.substring(len)).append(str.subSequence(0, len));
return sb.toString();
}
public static String DeleteStr(String str, int pos, int len)
{
StringBuffer sb = new StringBuffer();
sb.append(str.substring(0, pos-1)).append(str.substring(pos+len-1));
return sb.toString();
}
public static String DeleteStr(String str, char ch)
{
return str.replaceAll(String.valueOf(ch), "");
}
public static int getChineseNum(String str)
{
int count = 0;
byte[] array = str.getBytes();
count = array.length - str.length();
return count;
}
}
输出如下:
2
shandong from am I
gnodnahs morf ma I
gnodnahs from ma I
true
-1
cdefg
abefcfgcsd
abdeffgsd
456789123
789123456
10