Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "stringByReplacingCharactersInRange" method of NSString in Monotouch?

In Apple documentation for NSString there is a stringByReplacingCharactersInRange method, however I cannot find this method in Monotouch.

Is this method implementation missing from Monotouch - native API binding?

I need this method to implement some custom string handling logic inside shouldChangeCharactersInRange

like image 858
Yiannis Mpourkelis Avatar asked Nov 02 '11 11:11

Yiannis Mpourkelis


2 Answers

Here's some code to do what you need:

string text = field.Text;
string result;

result = text.Substring (0, range.Location) + replacement + text.Substring (range.Location + range.Length);
like image 163
jstedfast Avatar answered Sep 19 '22 06:09

jstedfast


Not everything is binded for NSString because it would mostly duplicate the .NET methods available in System.String.

The easiest way is to work on .NET (native) string and create an NSString from them if/when you need to interoperate with API that requires it. This has the advantages of minimizing the number of managed/unmanaged transitions (there's a small cost to that).

string s = "...";
var ns = new NSString (s);

Same goes if you receive a NSString from the API, convert it into a string then manipulate it.

NSString ns = NSSomething.GetIt ();
string s = ns.ToString ();

If you find a specific binding that has no similar .NET method then please fill a bug report on http://bugzilla.xamarin.com and we'll make sure to include it in future releases of MonoTouch. Often an immediate workaround can be given to unblock you.

like image 41
poupou Avatar answered Sep 19 '22 06:09

poupou