One, invert the string
package string;
public class Reverse {public static void main (string[] args) {String S1 = ' abc DEFG WF ';
Reverse string s2 = "" With string-character array;
Char[] cs = S1.tochararray ();
for (int i = cs.length-1 i >= 0; i--) {s2 = s2 + cs[i];
System.out.println ("Method one reverse operation is:" + s2);
Using StringBuffer's reverse () method to realize inversion stringbuffer sb = new StringBuffer (S1);
StringBuffer SB2 = Sb.reverse ();
System.out.println ("Method two reverse operation is:" + SB2);
Inversion String S3=myreverse (S1) is implemented using reverse storage;
System.out.println ("method three evils after the operation is:" + S3);
public static string Myreverse (String str) {char[] arr = new char[str.length ()];
int pos = Str.length ();
for (int x=0; x<str.length (); x + +) {Arr[x] = Str.charat (--pos);
str = string.copyvalueof (arr);
return str; }
}
Output:
Method A reverse operation is: FW gfed CBA
method Two reverse operation: FW gfed CBA
method Three evils after operation: FW gfed CBA
Two, the realization goes to the string two ends space function
Package string;
public class Trime {public
static void Main (string[] args) {
String str1 = " dwwg dwg wfg ";
Using the String class own method to remove both ends space
string str2 = Str1.trim ();
System.err.println ("Method One" +str2);
Custom method to remove both ends space
String Str3=mytrim (str1);
System.err.println ("Method Two" +STR3);
}
public static string Mytrim (String str) {
int start = 0, end = Str.length ()-1;
while (Start <= end && Str.charat (start) = = ")
start++;
While (the start <= end && Str.charat (end) = = ")
end--;
Return str.substring (Start, end + 1);
}
Output:
Method one DWWG dwg wfg
method two DWWG dwg WFG
Gets the number of times a string appears in another string and returns the starting index
Package string;
public class GetCount {public
static void Main (string[] args) {
String str = "sshelloworldhellojavahellojsp";
system.out.println ("count=" + getsubcount (str, "Hello"));
}
public static int Getsubcount (string str, string key) {
int count = 0;
int index = 0;
while (index = Str.indexof (key, index))!=-1) {
System.out.println ("index=" + index);
index = index + key.length ();
count++;
}
return count;
}
}
index=2
index=12
index=21
count=3
IndexOf returns the index value at the beginning of the substring in the long string, and returns 1.
The reverse of an English sentence
package string; import Java.util.Scanner; public class Reverse02 {public static string Reversewords (string sentence) {StringBuilder sb = new Stringbu
Ilder (Sentence.length () + 1);
string[] Words = Sentence.split ("");
for (int i = words.length-1 i >= 0; i--) {sb.append (Words[i]). Append (");
} sb.setlength (Sb.length ()-1);
return sb.tostring ();
public static void Main (string[] args) {Scanner in = new Scanner (system.in);
Holds the input string System.out.println ("Input test Case number n:");
string[] input = new string[in.nextint ()];
In.nextline ();
for (int i = 0; i < input.length i++) {Input[i] = In.nextline ();
}//output in reverse order after the English string System.out.println ("Output is:");
for (String s:input) {System.out.println (Reversewords (s)); }
}
}
Enter test Case number N:
2
Hello world!
I love you?
The output is:
world! Hello and love
i
Four, delete the most occurrences of characters in a string
Package string;
Import Java.util.HashMap;
Import Java.util.Map;
Import Java.util.Set;
public class Delmost {public static void main (string[] args) {String str = "AASSDFSCFW";
Map<character, integer> map = new Hashmap<character, integer> ();
char[] box = Str.tochararray ();
for (char ch:box) {Integer i = map.get (CH);
if (i = = null) {map.put (ch, 1);
else {map.put (ch, i + 1);
}//Create map key value pair Integer max = 0;
set<character> set = Map.keyset ();
for (Character Key:set) {Integer j = map.get (key);
if (Max < j) {max = J; }///The maximum number of times is passed to Max for (Character key:set) {if (Map.get (key) = max) {str
= Str.replaceall (Key.tostring (), "");
} System.out.println (str);
}
}
Aadfcfw
Enter a line of characters to count English letters, numbers and spaces.
Package string;
Import Java.util.Scanner;
public class Tongji {public
static void Main (string[] args) {
int eng=0,num=0,kong=0,elsel=0;
Scanner sc=new Scanner (system.in);
String str=sc.nextline ();
Char[] Ch=null;
Ch=str.tochararray ();
for (char c:ch) {
if (c>= ' 0 ' &&c<= ' 9 ') {
num++;
} else if ((c>= ' a ' &&c<= ' z ') | | (c>= ' A ' &&c<= ' Z ')) {
eng++;
} else if (c== ') {
kong++
} else{
elsel++
}
}
SYSTEM.OUT.PRINTLN ("Letter has" +eng);
SYSTEM.OUT.PRINTLN ("number has" +num);
SYSTEM.OUT.PRINTLN ("Space has" +kong);
System.out.println ("other" +elsel);
}
Hello World 378nihao has
3
spaces and 2
other 0