ruby 列表转换为字符串_Ruby程序将字符串转换为小写和大写

ruby 列表转换为字符串

将字符串转换为小写和大写 (Converting string into lowercase and uppercase)

Given a string and we have to convert it into lowercase and uppercase.

给定一个字符串,我们必须将其转换为小写和大写形式。

Lowercase or uppercase of a string can be easily printed or stored using upcase or downcase predefined methods. Using this method makes the code less complex but if you want to get through internal processing you should opt for method 2 where we have to find the uppercase or lowercase character of the specified character using its ASCII value.

可以使用大写小写预定义方法轻松地打印或存储字符串的小写或大写 。 使用此方法使代码不太复杂,但是如果要进行内部处理,则应该选择方法2,在该方法中,我们必须使用其ASCII值查找指定字符的大写或小写字符。

Methods used:

使用的方法:

  • puts: This method is used to put a string on the screen.

    puts :此方法用于在屏幕上放置字符串。

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

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

  • .size: .size method returns the size of the string.

    .size :.size方法返回字符串的大小。

  • .ord: This is a method which is used to convert the character into its ASCII equivalent.

    .ord :这是一种用于将字符转换为等效ASCII的方法。

  • .chr: .chr is used to convert the ASCII value into its character equivalent.

    .chr :.chr用于将ASCII值转换为等效的字符。

  • .upcase: This simply converts the string into uppercase. It capitalizes each alphabet in the string.

    .upcase :这只是将字符串转换为大写。 它大写字符串中的每个字母。

  • .downcase: This simply converts the string into its lowercase.

    .downcase :这只是将字符串转换为小写字母。

Ruby code to convert a string into uppercase and lowercase

Ruby代码将字符串转换为大写和小写

=begin 
Ruby program to convert a string into uppercase and lowercase
=end

# input the string
puts "Enter the string"
str=gets.chomp

#ask for the option 
puts "Choose the option: a)to lowercase b)to uppercase"
choice=gets.chomp

# condition to execute code based on the user's choice
if(choice=="a")
	i=0
	while(i!=str.size)
	    k=str[i].to_s
	    if(k>='A' and k<='Z')
	        str[i]=(k.ord+32).chr
        else
            str[i]=k
        end
	    i+=1
	end
	puts "The lowercase is #{str}"
else
	i=0
	while(i!=str.size)
	    k=str[i].to_s
	    if(k>='a' and k<='z')
	        str[i]=(k.ord-32).chr
	    else
	        str[i]=k;
	    end
	    i+=1
	end
	puts "The uppercase is #{str}"
end

Output

输出量

RUN1:
Enter the string
Hello world, how are you?
Choose the option: a)to lowercase b)to uppercase
a
The lowercase is hello world, how are you?

RUN2:
Enter the string
Hello world, how are you?
Choose the option: a)to lowercase b)to uppercase
b
The uppercase is HELLO WORLD, HOW ARE YOU?

In method 1, we are manipulating each alphabet using its ASCII value. The task gets a little complex but it is better to develop something from scratch for a great understanding of logic and concepts.

在方法1中,我们使用其ASCII值来处理每个字母。 任务有些复杂,但是最好是从头开始开发一些东西,以更好地理解逻辑和概念。

Method 2:

方法2:

=begin 
Ruby program to convert a string into 
uppercase or lowercase.
=end

# input the string
puts "Enter the string"
str=gets.chomp

#ask for the option 
puts "Choose the option: a)to lowercase b)to uppercase"
choice=gets.chomp

# condition to execute code based on the user's choice
if(choice=="a")
	str=str.downcase
	puts "The lowercase is #{str}"
else
	str=str.upcase
	puts "The uppercase is #{str}"
end

Output

输出量

RUN1:
Enter the string
Hello world, how are you?
Choose the option: a)to lowercase b)to uppercase
a
The lowercase is hello world, how are you?

RUN2:
Enter the string
Hello world, how are you?
Choose the option: a)to lowercase b)to uppercase
b
The uppercase is HELLO WORLD, HOW ARE YOU?


翻译自: https://www.includehelp.com/ruby/convert-the-string-into-lowercase-and-uppercase.aspx

ruby 列表转换为字符串

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值