Jorge Vega Sánchez
Jorge Vega Sánchez

Reputation: 7590

Substrings in Objective-C

I'm translating an example from C to Objective-C and have a questions.

C/C++ instruction:

buffer[i].str = population[i1].str.substr(0, spos) + population[i2].str.substr(spos, esize - spos);

How can I translate the part .substr(0, spos)

Buffer and population are vector using . str is a string variable i, i1, i2, spos and esize are integer variables.

Upvotes: 1

Views: 140

Answers (1)

Macmade
Macmade

Reputation: 53950

Use the NSString methods.

You've got a lot of methods for creating substrings.
Mainly:

  • substringFromIndex:
  • substringWithRange:
  • substringToIndex:

.substr(0, spos) may be translated to:

[ someString substringToIndex: spos ];

Note you will first need to convert your C++ string object to a NSString object.

Upvotes: 3

Related Questions