split( )
public class mySplit {
public static void main(String[] args) {
String str = "1,2,3,4,5,6,7,8";
mySplitFunc(str);
}
static void mySplitFunc(String str){
for (int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if ( c != ','){
System.out.print(c + " ");
}
}
}
}
结果:
compareTo( )
public class myCompareTo {
public static void main(String[] args) {
String str1 = "aabbccc";
String str2 = "aaaaaaaac";
if (myComTo(str1,str2) > 0){
System.out.println("字符串"+str1+"大");
}else if (myComTo(str1,str2) == 0 ){
System.out.println("字符串"+str1+"字符串"+str2+"一样大");
}else {
System.out.println("字符串"+str2+"大");
}
}
public static int myComTo(String str1, String str2){
int len1 = str1.length();
int len2 = str2.length();
int len = len1 > len2 ? len2 : len1;
for (int i = 0; i < len; i++) {
if (str1.charAt(i) - str2.charAt(i) < 0) {
return -1;
} else if (str1.charAt(i) - str2.charAt(i) > 0) {
return 1;
} else {
continue;
}
}
if (len1 == len2){
return 0;
}else {
return (len1 - len2) > 0 ? 1 : -1;
}
}
}
结果:
contains( )
public class myContains {
public static void main(String[] args) {
String str = "i am a student";
String find = "am";
if (myCons(str,find) != -1){
System.out.println("该字符串中包含字符"+find);
}else{
System.out.println("该字符串中未找到字符"+find);
}
}
public static int myCons(String src, String dst){
if (null == src || src.length() < 1 || null == dst || dst.length() < 1) {
return -1;
}
if (dst.length() > src.length()) {
return -1;
}
char[] srcArray = src.toCharArray();
char[] dstArray = dst.toCharArray();
int srcLen = srcArray.length;
int dstLen = dstArray.length;
for (int i = 0; i < srcLen; i++) {
boolean find = false;
if (srcArray[i] == dstArray[0] && (i + dstLen <= srcLen)) {
int equalCount = 0;
for (int j = 0; j < dstLen; j++) {
if (srcArray[i + j] == dstArray[j]) {
equalCount++;
}
}
if (equalCount == dstLen) {
find = true;
}
}
if (find) {
return 1;
}
}
return -1;
}
}
结果:
indexOf( )
public class myIndexOf {
public static void main(String[] args){
String str = "this is an apple";
String search = "apple";
System.out.println(myIdOfFunc(str,search));
}
public static int myIdOfFunc(String src, String dst){
if (null == src || src.length() < 1 || null == dst || dst.length() < 1) {
return -1;
}
if (dst.length() > src.length()) {
return -1;
}
char[] srcArray = src.toCharArray();
char[] dstArray = dst.toCharArray();
int srcLen = srcArray.length;
int dstLen = dstArray.length;
for (int i = 0; i < srcLen; i++) {
boolean find = false;
if (srcArray[i] == dstArray[0] && (i + dstLen <= srcLen)) {
int equalCount = 0;
for (int j = 0; j < dstLen; j++) {
if (srcArray[i + j] == dstArray[j]) {
equalCount++;
}
}
if (equalCount == dstLen) {
find = true;
}
}
if (find) {
return i;
}
}
return -1;
}
}
结果:
replace( )
public class myReplace {
public static void main(String[] args) {
String str = "An Apple";
myReplaceFunc(str,'A','a');
}
static void myReplaceFunc(String str,char sourChar,char newChar){
for (int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if (c == sourChar){
c = newChar;
}
System.out.print(c);
}
}
}
结果: