I want to replace the letter "e"
with the number "3"
. I want "elijah".my_method()
to return "3lijah"
.
I tried using sub
and gsub
methods along with replace
, but none of these replaces the letter. Here is my code:
class String
define_method(:leet_speak) do
containsE = self.include?("e")
whereIsE = self.index("e")
whereIsE.replace("3")
end
end
答案 0 :(得分:3)
The String#tr
method lets you do multiple transformations at once.
So if you wanted to do all the leet alphabet:
'elijah'.tr('abcdefghijklmnopqrstuvwxyz', '48cd3f9h1jklmn0pqr57uvwxy2')
returns:
3l1j4h
答案 1 :(得分:1)
None of the code you showed alters the original string (self
) or returns an altered string. So nothing happens.
Try it like this (I have no idea why you're using define_method
here, but I have no objection):
class String
define_method(:leet_speak) do
self.gsub("e","3")
end
end
s = "elijah".leet_speak()
puts s # => 3lijah
答案 2 :(得分:0)
case R.id.edt_d_o_birth: {
KeyboardUtils.hideSoftKeyboard(this);
Calendar calendar = Calendar.getInstance(Locale.getDefault());
DatePickerDialog datePickerDialog = new DatePickerDialog(RegisterActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
//todo
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
break;
}
String#tr 方法对您来说已经足够......