字符串indexOf(int ch)方法 (String indexOf(int ch) Method)

indexOf(int ch) is a String method in Java and it is used to get the index of a specified character in the string.

indexOf(int ch)是Java中的String方法,用于获取字符串中指定字符的索引。

If the character exists in the string, it returns the index of the first occurrence of the character, if the character does not exist in the string, it returns -1.

如果该字符存在于字符串中,则返回该字符首次出现的索引,如果该字符不存在于字符串中,则返回-1。

Syntax:

句法:

    int str_object.indexOf(int chr);

Here,

这里,

  • str_object is an object of main string in which we have to find the index of given character.

    str_object是主字符串的对象,我们必须在其中找到给定字符的索引。

  • chr is a character to be found in the string.

    chr是在字符串中找到的字符。

It accepts a character and returns an index of its first occurrence or -1 if the character does not exist in the string.

它接受一个字符并返回其首次出现的索引;如果字符串中不存在该字符,则返回-1。

Example:

例:

    Input:
String str = "IncludeHelp"
Function call:
str.indexOf('H')
Output:
7
Input:
String str = "IncludeHelp"
Function call:
str.indexOf('W')
Output:
-1

Java code to demonstrate the example of String.indexOf() method

Java代码演示String.indexOf()方法的示例

public class Main
{public static void main(String[] args) {String str = "IncludeHelp";
char ch;
int index;
ch = 'H';
index = str.indexOf(ch);
if(index != -1)
System.out.println(ch + " is found at " + index + " position.");
else
System.out.println(ch + " does not found.");
ch = 'e';
index = str.indexOf(ch);
if(index != -1)
System.out.println(ch + " is found at " + index + " position.");
else
System.out.println(ch + " does not found.");
ch = 'W';
index = str.indexOf(ch);
if(index != -1)
System.out.println(ch + " is found at " + index + " position.");
else
System.out.println(ch + " does not found.");
}
}

Output

输出量

H is found at 7 position.
e is found at 6 position.
W does not found.

翻译自: https://www.includehelp.com/java/string-indexOf-int-ch-method-with-example.aspx

Java String indexOf(int ch)方法与示例相关推荐

  1. Java String compareTo()方法与示例

    字符串compareTo()方法 (String compareTo() Method) compareTo() is a String method in Java and it is used t ...

  2. Java String startsWith()方法与示例

    字符串startsWith()方法 (String startsWith() Method) startsWith() method is a String class method, it is u ...

  3. java string.indexof(string)_Java StringBuffer indexOf()方法

    Java StringBuffer indexOf()方法 java.lang.StringBuffer.indexOf(String str, int fromIndex) 方法返回此字符串指定的子 ...

  4. Java String substring()方法示例

    Java String substring() method returns the substring of this string. This method always returns a ne ...

  5. Java String trim()方法示例

    Java String trim() method is used to remove leading and trailing whitespaces from a string. This met ...

  6. java遍历是什么意思_遍历Java String行的最佳方法是什么?

    遍历Java String行的最佳方法是什么? 目前,我正在使用类似: String[]lines = textContent.split(System.getProperty("line. ...

  7. Java String到int,Java int到String

    Today we will look at Java String to int conversion and then java int to String conversion. Java pro ...

  8. Java LinkedList addLast()方法与示例

    LinkedList addLast()方法 (LinkedList addLast() method) This method is available in package java.util.L ...

  9. Java LinkedList getFirst()方法与示例

    LinkedList getFirst()方法 (LinkedList getFirst() method) This method is available in package java.util ...

最新文章

  1. CCF NOI1040 除法游戏
  2. JQuery中隐式迭代和each的区别(通过反选功能解析)
  3. 计算机软硬件作品构思报告,计算机方面的实训报告
  4. python基础===八大排序算法的 Python 实现
  5. VS2010程序打包操作(超详细的)转
  6. [SpringSecurity]web权限方案_用户授权_注解使用
  7. 为什么Netty这么火?与Mina相比有什么优势?
  8. Python 学习笔记9(装饰器,decorator)
  9. java验证码Kaptcha
  10. 高炉计算机控制,一高炉计算机控制系统升级.pdf
  11. jquery设置表单元素只读_jquery设置元素readonly和disabled(checkbox只读)
  12. LeetCode 124. Binary Tree Maximum Path Sum
  13. 2.窗口大小改变和侧边栏收缩的时候,echarts图表的自适应
  14. Hive 内部表外部表
  15. Backbox Linux简介与配置内网IP
  16. js基础知识汇总03
  17. Nginx配置文件(作为Web服务器)
  18. 物联网基础建设-园区智能微电网设计方案
  19. 计算各个城市实际地区生产总值(附各个城市实际GDP)
  20. 拿下多家主机厂数百万前装定点,禾赛科技激光雷达量产进程加速

热门文章

  1. as400和java的区别_文件传输协议和AS400
  2. 跨域产生的原因和解决方法_板式家具开料机加工过程产生崩边原因及解决方法...
  3. import java.awt.BorderLayout;_Swing-布局管理器之BorderLayout(边界布局)-入门
  4. 生成一个GitHub的token用于git推送本地库至远程库
  5. Pytorch RuntimeERROR: Given groups=1 weights of size [256,64,1,1] expected input[1,16,256,256] to
  6. 在Ubuntu 8.04 LTS(hardy)下安装配置nginx和fastcgi方式的php
  7. PHP Smarty template for website
  8. html的学习思维导图
  9. UpdatePanel的内容中出现自定义多语言运行异常
  10. java已被弱化签名,高效Java第四十条建议:谨慎设计方法签名