ruby字符串截取字符串_Ruby程序检查字符串是否包含子字符串

ruby字符串截取字符串

检查字符串中的子字符串 (Checking substring in a string)

Given a string and substring, we have to check whether a string contains a substring or not.

给定字符串和子字符串,我们必须检查字符串是否包含子字符串。

A substring is a sequence of characters within a string or in other words, a substring is a part of the string. Ruby provides you a keyword through which you can check the availability of a substring. That keyword is known by ".include?" which takes the specified substring as the parameter under double quotes like .include? “Hello”.

子字符串是字符串中的字符序列,换句话说,子字符串是字符串的一部分。 Ruby为您提供了一个关键字,通过该关键字可以检查子字符串的可用性 。 该关键字被称为“ .include?”。 这将指定的子字符串作为双引号(如.include)下的参数? “你好”。

Methods used:

使用的方法:

  • puts: The method put string is used to put a message on the screen for the user.

    puts :put字​​符串方法用于向用户在屏幕上显示消息。

  • gets: This method is used to take input from the user.

    gets :此方法用于接收用户的输入。

  • .include?: One of the functionalities of this keyword is that it is used to analyze the existence of a substring within a string.

    。包括? :此关键字的功能之一是用于分析字符串中子字符串的存在。

Variables used:

使用的变量:

  • str1: It is being employed for storing the actual string.

    str1 :用于存储实际的字符串。

  • substr2: It is storing the substring to be checked which is provided by the user.

    substr2 :存储用户提供的要检查的子字符串。

Ruby code to check whether a string contains a substring or not

Ruby代码检查字符串是否包含子字符串

=begin 
Ruby program to check whether a string 
contains substring or not
=end

puts "Enter the string:"
str1=gets.chomp
puts "Enter the substring:"
substr1=gets.chomp

if str1.include? substr1
	puts "Substring Found"
else
	puts "substring Not Found"
end

Output

输出量

Run 1:
Enter the string:
My name is Hrithik
Enter the substring:
saksham
substring Not Found

Run 2: 
Enter the string:
You are at Includehelp.com
Enter the substring:
at
Substring Found


翻译自: https://www.includehelp.com/ruby/check-whether-a-string-contains-substring-or-not.aspx

ruby字符串截取字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值