Java练习(第5天)【总结】在字符串中寻找特定的字符(5种方法)

14 篇文章 0 订阅
本文介绍了Java中的四种字符串查找方法:indexOf用于首次出现位置,lastIndexOf用于最后一次出现位置,indexOf在指定位置后的首次出现,以及lastIndexOf在指定位置前的末次出现。同时提及charAt用于获取指定位置的字符,注意处理边界情况。
摘要由CSDN通过智能技术生成

问题描述:在字符串中寻找特定字符

1、第1次出现位置

实现函数原型: int indexOf(char c)

Java代码:

import java.io.*;
public class Way_1
{
    public static void main(String args[])
	 {
			String str = "Geeks for Geeks is a computer science portal";
			System.out.println(str.length());
			
			int firstIndex = str.indexOf('s');
			System.out.println("First occurrence of char 's' is found at: " + firstIndex);
			
			int firstIn = str.indexOf('z');
			System.out.println("First occurrence of char 'z' is found at: " + firstIn);        
    }
}

样例字符串: Geeks for Geeks is a computer science portal

样例输出:

在这里插入图片描述

2、最后一次出现位置

实现函数原型: public int lastIndexOf(char c)

Java代码:

import java.io.*;
public class Way_2
{
    public static void main(String args[])
	 {
			String str = "Geeks for Geeks is a computer science portal";
			System.out.println(str.length());
			
			int lastIndex = str.lastIndexOf('s');
			System.out.println("Last occurrence of char 's' is found at: " + lastIndex);			
			int lastIn = str.lastIndexOf('z');
			System.out.println("Last occurrence of char 'z' is found at: " + lastIn);        
    }
}

样例字符串: Geeks for Geeks is a computer science portal

样例输出:

在这里插入图片描述

3、指定位置后的首次出现

实现函数原型: public int IndexOf(char c, int indexFrom)

说明: 除-1外,该函数的返回值必然大于或等于其第2个参数indexFrom

Java代码:

import java.io.*;

public class String_Search
{
    public static void main(String [] args)
    {
        String str = "Geeks For Geeks is a computer  science portal";
        System.out.println(str.length());
        
        int first_in = str.indexOf('s', 10);
        System.out.println("First occurrence of char 's' after index 10: " + first_in);
         int first_not_in = str.indexOf('z', 10);
        System.out.println("First occurrence of char 'z' after index 10: " + first_not_in);
    }
}

样例字符串: Geeks For Geeks is a computer science portal

样例输出:

在这里插入图片描述

4、指定位置前的末次出现

实现函数原型: public int lastIndexOf(char c, int fromIndex)

说明: 除-1外,该函数的返回值必然小于或等于其第2个参数fromIndex

Java代码:

import java.io.*;

public class String_Search_2
{
    public static void main(String [] args)
    {
        String str = "Geeks For Geeks is a computer science portal";
        System.out.println(str.length());

        int lastIndex = str.lastIndexOf('s', 20);
        System.out.println("Last occurrence of char 's' before index 20: " + lastIndex);
        int last_not_in = str.lastIndexOf('z', 20);
        System.out.println("Last occurrence of char 'z' before index 20: " + last_not_in);
    }
}

样例字符串: Geeks For Geeks is a computer science portal

样例输出:

在这里插入图片描述
5、指定位置的字符

实现函数原型: char charAt(int indexNumber)

说明: 若指定的下标indexNumber超出字符串长度范围,则抛出StringIndexOutOfBounds异常。

Java代码:

import java.io.*;

public class Character_At
{
    public static void main(String [] args)
    {
        String str = "Geeks For Geeks is a computer science portal";
        System.out.println(str.length());

        int char_at = str.charAt(20);
        System.out.println("Character at location 20: " + char_at);
        
        int char_not_at = str.charAt(60);
    }
}

样例字符串: Geeks For Geeks is a computer science portal

样例输出:

在这里插入图片描述

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不是AI

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值