2.1 Using the String API
Strings are the most common type of java. And is a composite type
Strings are very often used, and their best APIs are familiar with documents.
Infer that there are no repeated characters in the string
String s = "abcdebxyz";
Infer that there are no repeated characters
/* Infer that there are no repeated characters in string s = "abcdebxyz"; infer that there are no repeated characters */package stringapi; public class Judgerepeatinstring {public static void Main (string[] args) { String s = "Abcdex ah yz"; Boolean judge = false;//Inference token, initialization is not repeated for (int i=0;i<s.length (); i++) {//} first traversal of this string char c = s.charat (i);// Get the character I-bit if (S.lastindexof (c)!=i) {//Assume that the last occurrence of C in the string is the position of the character in the!=i judge = true;//has repeated } } SYSTEM.OUT.PRINTLN (judge); }}
True
char
|
charAt(int index) Returns the value at the specified index char .
|
int
|
lastIndexOf(int ch) Returns the index of the specified character at the last occurrence in this string.
|
Shift Encryption
ASCII code + one number
The definition of a string cannot be changed.
The so-called change, is reborn into a new string
Assuming a large number of freshmen into new strings, there is great pressure on the computer
So consider turning the string into an array of char types. After the shift is encrypted, it becomes a string and can.
/* Shift-encrypted ASCII + A string of definitions is the so-called change that cannot be changed, is a new string of freshmen into the new string hypothesis, a large number of freshmen to the computer has a very large pressure, so consider the string into a char type array, shift encryption after the string. We can. * /Package Stringapi, public class Movesecret {public static void Main (string[] args) { String s = "Where is Is my code Chinese very messy? "; Char c[] = S.tochararray ();//The string is converted to a char array for (int i=0;i<c.length;i++) { c[i]++; } String s2 = new String (c),//The constructor of the string is referred to as the char array. SYSTEM.OUT.PRINTLN (s2); }}
[Email protected] 丮 斈 slouching Baja 乲 吘 @
char[]
|
toCharArray () Converts this string to a new character array.
|
String (char[] value) Assign a new one String . Causes it to represent the sequence of characters currently included in the character array parameter.
|
Infer
Infer if a full path name is in the C drive and is an. ini extension.
"C:\\xyz\\bak\\x.ini" composite standard
"Ccc\\ttt\\kk\\ini" is not
/* Inference infers whether a full pathname is in the C drive and is an. ini extension."C:\\xyz\\bak\\x.ini" composite Standard "Ccc\\ttt\\kk\\ini" is not */package stringapi; public class Judgepath {public static void Main (string[] args) { //String s = "Ccc\\ttt\\kk\\ini"; String s = "C:\\xyz\\bak\\x.ini"; Boolean judge = false; if (S.startswith ("c:\\") && s.endswith (". ini")) {//start with "c:\\" && End with ". ini" judge = true; } SYSTEM.OUT.PRINTLN (judge); }} True
boolean
|
startsWith (String prefix) Test whether this string starts with the specified prefix.
|
boolean
|
endsWith(String suffix) Tests whether this string ends with the specified suffix.
|
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Programming algorithm base-2.1 using the string API