Ruby String Replacement, Substitution and Insertion
来源:互联网 发布:安卓盒子软件 编辑:程序博客网 时间:2024/06/09 06:29
Changing a Section of a String
Ruby allows part of a string to be modified through the use of the []= method. To use this method, simply pass through the string of characters to be replaced to the method and assign the new string. As is often the case, this is best explained through the use of an example:
myString = "Welcome to JavaScript!"
myString["JavaScript"]= "Ruby"
puts myString
=> "Welcome to Ruby!"
As you can see, we replaced the word "JavaScript" with "Ruby".
The []= method can also be passed an index representing the index at with the replacement is to take place. In this instance the single character at the specified location is replaced by the designated string:
myString = "Welcome to JavaScript!"
myString[10]= "Ruby"
puts myString
=> "Welcome toRubyJavaScript!"
Perhaps a more useful trick is to specify an index range to be replaced. For example we can replace the characters from index 8 through to index 20 inclusive:
myString = "Welcome to JavaScript!"
=> "Welcome to JavaScript!"
myString[8..20]= "Ruby"
=> "Ruby"
puts myString
=> "Welcome Ruby!"
Ruby String Substitution
The gsub and gsub! methods provide another quick and easy way of replacing a substring with another string. These methods take two arguments, the search string and the replacement string. The gsub method returns a modified string, leaving the original string unchanged, whereas the gsub! method directly modify the string object on which the method was called:
myString = "Welcome to PHP Essentials!"
=> "Welcome to PHP Essentials!"
myString.gsub("PHP", "Ruby")
=> "Welcome to Ruby Essentials!"
An entire string, as opposed to a substring, may be replaced using the replace method:
myString = "Welcome to PHP!"
=> "Welcome to PHP!"
myString.replace "Goodbye to PHP!"
=> "Goodbye to PHP!"
Repeating Ruby Strings
A string may be multiplied using the * method (not something I have ever needed to do myself, but the capability is there if you need it):
myString = "Is that an echo? "
=> "Is that an echo? "
myString * 3
=> "Is that an echo? Is that an echo? Is that an echo? "
Inserting Text into a Ruby String
@import "/extensions/GoogleAdSense/GoogleAdSense.css";
So far in this chapter we have looked exclusively at changing the existing text contained in a Ruby string object. Another common requirement is to insert new text at a certain location in a string. This is achieved in Ruby using the insert method. The insert method takes as arguments index position into the string where the insertion is take place, followed by the string to be inserted
myString = "Paris in Spring"
myString.insert 8, " the"
=> "Paris in the Spring"
Ruby chomp and chop Methods
The purpose of the chop method is to remove the trailing character from a string:
myString = "Paris in the Spring!"
=> "Paris in the Spring!"
myString.chop
=> "Paris in the Spring"
Note that chop returns a modified string, leaving the original string object unchanged. Use chop! to have the change applied to the string object on which the method was called.
The chomp method removes record separators from a string. The record separator is defined by the $/ variable and is, by default, the new line character (/n). As with the chop method the chomp! variant of the method applies the change to string object on which the method is called:
myString = "Please keep/n off the/n grass"
=> "Please keep/n off the/n grass/n"
myString.chomp!
=> "Please keep/n off the/n grass"
Reversing the Characters in a String
The reverse method is used to reverse the contents of a string:
myString = "Paris in the Spring"
=> "Paris in the Spring"
myString.reverse
=> "gnirpS eht ni siraP"
Once again, not something I've ever needed to do in all my years as a developer, but you never know when you might need to do it.
- Ruby String Replacement, Substitution and Insertion
- ruby String substitution
- string substitution
- json and string (Ruby)
- String substitution in scripts
- Vi string replacement
- Phone String Replacement
- replaceAll(String regex, String replacement)
- Replacement
- * String类的public String replaceAll(String regex,String replacement)
- ruby String
- leetcode A String Replacement Problem---流程图
- Method Replacement for Fun and Profit
- new operator,operator new and replacement new
- String.replaceAll(String regex, String replacement) 方法的使用
- Insertion Sort and Merge Sort
- Insertion sort and Merge sort
- Insertion Operator and Extraction Operator
- 输入输出命令的邦定过程及数据发送
- 窗体控件线程安全类
- 数据库查询出的文本有格式的显示到页面的问题
- Asp.Net中防止出现用户重复登陆
- 北大ACM题目分类1(适合树图专题)
- Ruby String Replacement, Substitution and Insertion
- Windows快捷方式文件格式详解(附代码)
- 在线刷网站流量(倍儿厉害)
- make progress everyday
- WEB系统防止拷贝编译的一点方法
- PHP常用函数手册
- 在Oracle 10g有几种查找数据的方法
- Levenshtein Distance 算法
- Auto Build Texture Coordinate