In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge about "how to use String string data type in Java". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
String commonly used methods, I in the form of code, to illustrate these commonly used methods.
@Test public void test1(){ //1. Return the length of the string String s1 = "helloworld"; System.out.println(s1.length()); //2. Return characters at an index System.out.println(s1.charAt(1)); //3. Determine whether the string is empty string System.out.println(s1.isEmpty()); //4. Convert all strings in String to lowercase String s2 = "ShoPPing"; String s3 = s2.toLowerCase(); System.out.println(s3); //5. Convert all strings in String to uppercase String s4 = s2.toUpperCase(); System.out.println(s4); //6. Returns a copy of the string, ignoring leading and trailing whitespace String s5 = " An dro id "; String s6 = s5.trim(); System.out.println("**********"+s5+"**********"); System.out.println("**********"+s6+"**********"); //7. Compare string contents to see if they are the same System.out.println(s1.equals(s5)); //8. Similar to equals method, this ignores case String s7="abcDef"; String s8="ABCDef"; System.out.println(s7.equals(s8));//false System.out.println(s7.equalsIgnoreCase(s8));//true //9. Concatenates the specified string to the end of this string, equivalent to "+" String s9="abc"; String s10 = s9.concat("def"); System.out.println(s10); //10. Compare the sizes of two strings String s11="abe"; System.out.println(s9.compareTo(s11)); //-2 indicates that s9 is less than s11 //11. Returns a new string, which is a substring of this string truncated from bedinIndex to the last. String s12 = "I must learn Android"; System.out.println(s12.substring(6)); //12. Returns a new string that is a substring of this string truncated from initialIndex to endIndex(excluding) String s13 = s12.substring(2, 6); System.out.println(s13); }
Of course, in String, there are more than these methods, but these are the more common methods.
Here are some other ways:
Take the code as an example:
@Test public void test2(){ //1. Tests whether this string ends with the specified suffix String s1 = "helloworld"; System.out.println(s1.endsWith("ld"));//true //2. Tests whether this string ends with the specified prefix System.out.println(s1.startsWith("hel"));//true //3. Tests whether this string starts with the specified prefix System.out.println(s1.startsWith("ow", 4));//true //4. Returns true if and only if this string contains the specified char value sequence; System.out.println(s1.contains("lo"));//true System.out.println(s1.contains("lowld"));//false //5. Returns the index of the first occurrence of the specified substring in this string System.out.println(s1.indexOf("el"));//1 //6. Returns the index of the first occurrence of the specified substring in this string, starting at the specified index System.out.println(s1.indexOf("ow",3));//4 //7. Returns the index of the rightmost occurrence of the specified substring in this string System.out.println(s1.lastIndexOf("o"));//6 //8. Returns the index of the last occurrence of the specified substring in this string, searching backwards from the specified index System.out.println(s1.lastIndexOf("o", 5));//4 }
Here are the regular methods in String:
@Test public void test3(){ //1. Returns a new string obtained by replacing all oldChar occurrences in this string with newChar String s1="Hello, I am a programmer, Xiaobai! "; System.out.println(s1.replace ('small ',' big')); //2. Replace all substrings of this string that match the literal target sequence with the specified literal replacement sequence System.out.println(s1.replace("small white","big yellow")); //3. Replace all substrings of this string that match the given regular expression with the given replacement String s2="12Hello2342World234Android"; String s3 = s2.replaceAll("\d+", ",").replaceAll("^,|,$", ""); System.out.println(s3); //4. Tells if this string matches the given regular expression String s4="123456"; //Determine whether the s4 string consists of numbers, i.e. 1-n numbers boolean matches = s4.matches("\d+"); System.out.println(matches); String tel="0373-12345678"; //Determine if this is a landline phone in Henan boolean matches1 = tel.matches("0373-\d{7,8}"); System.out.println(matches1); //5. Split this string according to the match of the given regular expression String s5="hello|world|android"; String[] split = s5.split("\|"); for (int i = 0; i < split.length; i++) { System.out.println(split[i]); } System.out.println("****************************"); //6. Split the string according to the matching regular expression. The maximum number of characters cannot exceed the limit. If it exceeds the limit, the rest will be placed in the last element. String s6="hello.world.android"; String[] split1 = s6.split("\. "); for (int i = 0; i < split1.length; i++) { System.out.println(split1[i]); } }"How to use String data type in Java" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.