Java中String indexOf() 方法解读 2021-12-07 20:13 400阅读 0赞 翻译自[https://www.geeksforgeeks.org/java-string-indexof/][https_www.geeksforgeeks.org_java-string-indexof] **1 String indexOf(char c)**:此方法返回**c字符在string中的下标**【不存在则返回-1】 解释代码: // Java code to demonstrate the working // of String indexOf() public class Index1 { public static void main(String args[]) { // Initialising String String gfg = new String("Welcome to geeksforgeeks"); System.out.print("Found g first at position : "); // Initial index of 'g' will print // prints 11 System.out.println(gfg.indexOf('g')); } } 输出: Found g first at position : 11 **2 String indexOf(char c, int strt)**:此方法**从**下标**strt处开始查找c字符在string中的下标**【不存在则返回-1】 解释代码: // Java code to demonstrate the working // of String indexOf(char ch, int strt) public class Index2 { public static void main(String args[]) { // Initialising String String gfg = new String("Welcome to geeksforgeeks"); System.out.print("Found g after 13th index at position : "); // 2nd index of 'g' will print // prints 19 System.out.println(gfg.indexOf('g', 13)); } } 输出: Found g after 13th index at position : 19 **3 String indexOf(String str)**:此方法返回**字符串在string中的下标**【不存在则返回-1】 解释代码: // Java code to demonstrate the working // of String indexOf(String str) public class Index3 { public static void main(String args[]) { // Initialising string String Str = new String("Welcome to geeksforgeeks"); // Initialising search string String subst = new String("geeks"); // print the index of initial character // of Substring // prints 11 System.out.print("Found geeks starting at position : "); System.out.print(Str.indexOf(subst)); } } 输出: Found geeks starting at position : 11 **4 String indexOf(String str, int strt)**:此方法**从**下标**strt处开始查找字符str在string中的下标**【不存在则返回-1】 解释代码: // Java code to demonstrate the working // of String indexOf(String str, int strt) public class Index4 { public static void main(String args[]) { // Initialising string String Str = new String("Welcome to geeksforgeeks"); // Initialising search string String subst = new String("geeks"); // print the index of initial character // of Substring aftr 14th position // prints 19 System.out.print("Found geeks(after 14th index) starting at position : "); System.out.print(Str.indexOf(subst, 14)); } } 输出: Found geeks(after 14th index) starting at position : 19 **5 其他相关的应用** Finding out if a given character (maybe anything upper or lower case) is a vowel or consonant. Implementation is given below: 解释代码 class Vowels { // function to check if the passed // character is a vovel public static boolean vowel(char c) { return "aeiouAEIOU".indexOf(c)>=0; } // Driver program public static void main(String[] args) { boolean isVowel = vowel('a'); // Printing the output if(isVowel) System.out.println("Vowel"); else System.out.println("Consonant"); } } 输出: Vowel [https_www.geeksforgeeks.org_java-string-indexof]: https://www.geeksforgeeks.org/java-string-indexof/ 文章版权声明:注明蒲公英云原创文章,转载或复制请以超链接形式并注明出处。
相关 Java中字符串indexof()的使用方法 有四种方法可以在Java(indexof())中找到字符串中的子字符串 indexOf方法返回一个整数值,该值指示子字符串在String对象中的开始位置。如果未找到子字... 朱雀/ 2021年05月23日 17:10/ 10 赞/ 10252 阅读
相关 Java中字符串indexOf()的使用方法 Java中字符串中子串的查找共有四种方法(indexof()) indexOf 方法返回一个整数值,指出 String 对象内子字符串的开始位置。如果没有找到子 痛定思痛。/ 2021年09月15日 10:40/ 0 赞/ 468 阅读
相关 Java中String indexOf() 方法解读 翻译自[https://www.geeksforgeeks.org/java-string-indexof/][https_www.geeksforgeeks.org_java 朱雀/ 2021年12月07日 20:13/ 0 赞/ 401 阅读
相关 解读ArrayList中的indexOf(Object o)方法 源码如下: / Returns the index of the first occurrence of the specified elemen 小鱼儿/ 2022年03月28日 23:12/ 0 赞/ 120 阅读
相关 java练习——int indexOf(String ,int) 救基友记2 Time Limit: 1000 ms Memory Limit: 65536 KiB [Submit][] [Statistic][] Problem 刺骨的言语ヽ痛彻心扉/ 2022年05月26日 06:43/ 0 赞/ 40 阅读
相关 Java String indexOf()方法 Java String indexOf()方法 ¥ 我要打赏 [Java教程][Java] 作者:freeself 评论:0 条 Java技术QQ群:[22727 今天药忘吃喽~/ 2022年06月01日 08:47/ 0 赞/ 92 阅读
相关 C# String.IndexOf 方法 [C\ String.IndexOf 方法][C_ String.IndexOf] Posted on 2010-07-19 17:52 [ ykhi][ykhi] 阅读( 1 小咪咪/ 2022年07月16日 13:23/ 0 赞/ 40 阅读
相关 java String 查找字符串是否存在方法 --> IndexOf 用法 IndexOf 用法 indexOf(Srting s) 从头开始检索 indexOf(String s ,int startpoint) 从startpoint处开 电玩女神/ 2022年07月19日 03:30/ 0 赞/ 436 阅读
相关 JavaScript indexOf() 方法 定义和用法 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。 语法 stringObject.indexOf(searchval 待我称王封你为后i/ 2022年08月04日 13:24/ 0 赞/ 72 阅读
相关 JS中string方法中常用方法之八:String.prototype.indexOf() String.prototype.indexOf()返回指定值在字符串对象中首次出现的位置。 从 fromIndex 位置开始查找,如果不存在,则返回 -1。 语法:s Myth丶恋晨/ 2022年09月26日 09:59/ 0 赞/ 34 阅读
还没有评论,来说两句吧...