String Question

Discussion in 'iPhone Tips, Help and Troubleshooting' started by dareuhl, May 24, 2008.

  1. dareuhl macrumors newbie

    Joined:
    May 24, 2008
    #1
    Im trying to figure how to do this, which is written in C#, in objective C....

    public static string Mid(string param,int startIndex)
    {
    //start at the specified index and return all characters after it
    //and assign it to a variable
    string result = param.Substring(startIndex);
    //return the result of the operation
    return result;
    }

    thanks in advance

    basically i want to get all characters after a specified character.
     
  2. pranavss11 macrumors 6502

    Joined:
    Dec 29, 2007
    Location:
    San Jose
    #2
    The code looks fine I think, but I could be wrong its 4.20 AM. So whats the problem? Is the left to right thing?

    //assign a value to our string
    string myString = "This is a string";
    //get 4 characters starting from the left
    Console.WriteLine(Left(myString,4));
    //get 6 characters starting from the right
    Console.WriteLine(Right(myString,6));
    //get 4 characters starting at index 5 of the string
    Console.WriteLine(Mid(myString,5,4));
    //get the characters from index 5 up to the end of the string
    Console.WriteLine(Mid(myString,5));
    //display the result to the screen Console.ReadLine();
     
  3. mysticwhiskey macrumors newbie

    Joined:
    Mar 31, 2008
    #3
    Hi,

    I think -[NSString substringFromIndex] will do what you want:

    Code:
    NSString* string1 = @"ABCDEF";
    NSString* string2 = [string1 substringFromIndex:3];    // string2 should be "DEF"
    
    I'm not at my Mac now, so I can't test this. Let us know if it works.

    BTW: This thread should be in the programming section, not the iPhone one... ;)
     

Share This Page