获取两个字符串中最大相同子串
public class MaxSubString {
public static List < String > getMaxSubString ( String str, String target) {
String maxStr = ( str. length ( ) > target. length ( ) ) ? str : target;
String minStr = ( str. length ( ) < target. length ( ) ) ? str : target;
int len = minStr. length ( ) ;
List < String > list = new ArrayList < > ( ) ;
for ( int i = 0 ; i < len; i++ ) {
for ( int x = 0 , y = len - i; y <= len; x++ , y++ ) {
String str1 = minStr. substring ( x, y) ;
if ( maxStr. contains ( str1) ) {
list. add ( str1) ;
}
}
if ( list. size ( ) != 0 ) {
return list;
}
}
return null ;
}
public static void main ( String [ ] args) {
List < String > maxSubString = getMaxSubString ( "abcwerthelloyuiodef" , "cvhellobnm" ) ;
for ( String s : maxSubString) {
System . out. println ( s) ;
}
System . out. println ( maxSubString. toString ( ) ) ;
}
}
子串在父串出现次数
public class OccurNum {
public static void main ( String [ ] args) {
String str = "java c js python R java scala c++ " ;
String targetStr = "java" ;
int targetStrLen = targetStr. length ( ) ;
int beginIndex = 0 ;
int count = 0 ;
while ( true ) {
int first = str. indexOf ( targetStr, beginIndex) ;
if ( first >= 0 ) {
count ++ ;
beginIndex = first + targetStrLen;
} else {
break ;
}
}
System . out. println ( count) ;
}
}
子串在父串出现次数2
public class OccurNum2 {
public static Integer occur ( String str, String target) {
int count = 0 ;
int len;
while ( ( len = str. indexOf ( target) ) != - 1 ) {
count++ ;
str = str. substring ( len + target. length ( ) ) ;
}
return count;
}
public static void main ( String [ ] args) {
Integer num = occur ( "java c js python R java scala c++ " , "java" ) ;
System . out. println ( num) ;
}
}
字符串自定义下标反转
public class ReverseStr {
public static String reverse ( String str, int start, int end) {
char [ ] chars = str. toCharArray ( ) ;
for ( int i = start, j = end; i < j ; i++ , j-- ) {
char temp = chars[ i] ;
chars[ i] = chars[ j] ;
chars[ j] = temp;
}
return new String ( chars) ;
}
public static void main ( String [ ] args) {
String reverse = reverse ( "javascalac++" , 3 , 7 ) ;
System . out. println ( reverse) ;
}
}
字符串自定义下标反转2
public class ReverseStr2 {
public static String reverse ( String str, int start, int end) {
String str1 = str. substring ( 0 , start) ;
for ( int i = end; i >= start; i-- ) {
char c = str. charAt ( i) ;
str1 += c;
}
str1 += str. substring ( end+ 1 ) ;
return str1;
}
public static void main ( String [ ] args) {
String reverse = reverse ( "javascalac++" , 3 , 7 ) ;
System . out. println ( reverse) ;
}
}
字符串自然排序
public class Sort {
public static String sort ( String str) {
char [ ] chars = str. toCharArray ( ) ;
Arrays . sort ( chars) ;
return new String ( chars) ;
}
public static void main ( String [ ] args) {
String sort = sort ( "5235" ) ;
System . out. println ( sort) ;
}
}
实现String类的trim()
public class trimDemo {
public static void main ( String [ ] args) {
String str = " java xxx www " ;
int strLen = str. length ( ) ;
int headIndex = 0 , trialIndex = 0 ;
for ( int i = 0 ; i < strLen; i++ ) {
if ( str. charAt ( i) != ' ' ) {
headIndex = i;
break ;
}
}
for ( int i = strLen- 1 ; i >= 0 ; i-- ) {
if ( str. charAt ( i) != ' ' ) {
trialIndex = i;
break ;
}
}
System . out. println ( headIndex) ;
System . out. println ( trialIndex) ;
String resStr = "" ;
for ( int i = headIndex; i <= trialIndex; i++ ) {
resStr += str. charAt ( i) ;
}
String resStr2 = str. substring ( headIndex, trialIndex+ 1 ) ;
System . out. println ( resStr) ;
System . out. println ( resStr2) ;
}
}
实现String类的trim()2
public class TrimDemo2 {
public static String myTrim ( String str) {
int start = 0 ;
int end = str. length ( ) - 1 ;
while ( start < end && str. charAt ( start) == ' ' ) {
start ++ ;
}
while ( start < end && str. charAt ( end) == ' ' ) {
end -- ;
}
return str. substring ( start, end + 1 ) ;
}
public static void main ( String [ ] args) {
String res = myTrim ( " java xx scala " ) ;
System . out. println ( res) ;
}
}
下划线命名转为驼峰命名
private static void testStr ( ) {
StringBuilder str = new StringBuilder ( "p1_roughness_eval" ) ;
int i = 0 ;
StringBuilder replaced = str;
while ( ( i = str. indexOf ( "_" , i) ) != - 1 ) {
String replaceStr = String . valueOf ( str. charAt ( i + 1 ) ) . toUpperCase ( ) ;
replaced = str. replace ( i+ 1 , i+ 2 , replaceStr) ;
str. replace ( i, i+ 1 , "" ) ;
i+= 1 ;
}
System . out. println ( replaced) ;
}