Ruby :String
As with Any Class with Ruby the way to create a new instance of the class would be thename.new.so for String as well we can create a new String using a = String.new. a.class would give the “String”.Another way/normal way to create string is to assign something between double quote.You can also use single quote. The difference is the \n will be interpretted by double quote .so when printing using puts it will print a new line where a string which is defined using single quote will not.
Double quote can be replace by % followed by any delimiting character .till the appearance of that delimiting character again the inbetween stuff is taken as a string.when the string flows across several lines then use << followed by any delimiting characters.
eg <<29 testing the hello world
my world
29
%Q is equivalent for double quote and %q for single quote
Some useful methods.
1. Is the string empty ? a.empty?
2.Measure the size / length of String –> a.length / a.size.
3.String is nothing but a array of character.So the Array operation can be performed on String . Slice is the equivalent of [].a.slice(0)/a[0] would give the first character in the string but as ascii and to convert it you should try .chr method.(this is not a problem for other operations) other operations like a.slice(10,5)/a[10,5] –> string starting at 10 (offset) and a length of 5 characters. and you can also use range a[12..17]/[12...17] will pull the character 12th to 17th character .three dots means exclude the last character in range .you use regular expression in array of string a[/pattern/] .
4.<< / + / concat ->operator overloading for << and + to perform concatenation of strings.In case of operator overloading there need to be a space between the object and the parameter for eg “hello” << “mani” or “hello” + “mani” .Incase concat it is just like any other method..
5. You can freeze the string ie., will not allow the string to changed.You can check this by frozen?
6.To find the position / whether it is available use index a.index(“k”) will give the position if found else nil.
7.eql?/ == / === /casecmp compares string. if it is operator overloading then space on both sides. === compares the type of the compared objects as well.casecmp ignores case while comparing.
8.to really compare ie., which is bigger… use the spaceship operator <=> . if -1 then the object on right side is bigger (it is like 67 – 68 will -1) if equal then it will be 0 if left side is bigger then it will be +ve.
9. string *2 => string is nothing but string + string. so “hello” * 2 = “hellohello”
10.insert in a string is easy just call insert followed by index position (this is array index meaining it starts at 0)
11.change the value in the String . a[]=/ splice!() .so it is simple just choose the character you want to replace using [] and on right put the character that you want .if you don’t want any character replaced then just used insert.
12.chomp/chop -> chomp take the new line character only / any character that you pass to it.chop chops off any character at the last.
13.delete is for deleting all the particular characters on the string. “all”.delete”l” => a
14.sub/gsub/replace : substitute / global substitute the string. difference between assign and replace is the object id retained for replace….
15.reverse..
16.split : splits the string to an array with the delimter string passed. if you want to split everycharacter then a.split(//).
17.Iterate over the string => .each/each_byte (each_byte is byte wise .so it needs a chr)/each_line: to loop over each line
18.upcase/downcase/swapcase/capitalize
19.center/ljust/rjust to pad it with space if character is passed then the character is padded.
20.strip/lstrip/rstrip to strip off the space
21.squeeze –> remove duplicates .if parameter is passed then that duplicates will be removed.
22. incrementing string => next|succ/upto/.. : next|succ would increment the last character by 1. “a”.upto “z”. for i in “a”..”z”
23 to_s / to_sym: to convert to String/ Symbol
24. “string” % argument -> “%15s” % “hello”
25. “string” =~ /pattern/ .scan -> a.scan(/pattern/) => gives a array based on pattern “happy world”.scan(/(..)(..)/). MatchData[0] -> first matched word.
26. Counts the no of characters in the parameter.you can pass more that 1 character.It will give the sum of count.
27. crypt : encrypt the string “st”.encrypt “@1″
28. tr : replace the string with passed parameter and delete duplicates.
starts_with?/ends_with?
clear
partition/rpartion …to paration a string on a given parameter…