English 中文(简体)
缩略语
原标题:Replace characters in NSString

I am trying to replace all characters except last 4 in a String with * s.
In objective-c there is a method in NSString class replaceStringWithCharactersInRange: withString: where I would give it range (0,[string length]-4) ) with string @"*". This is what it does: 123456789ABCD is modified to *ABCD while I am looking to make ********ABCD. I understand that it replaced range I specified with string object. How to accomplish this ?

最佳回答
NSError *error;

NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\d" options:NSRegularExpressionCaseInsensitive error:&error];

NSString *newString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"*"];
问题回答

This looks like a simple problem... get the first part string and return it with the last four characters appended to it.
Here is a function that returns the needed string :

-(NSString *)neededStringWithString:(NSString *)aString {
    // if the string has less than or 4 characters, return nil
    if([aString length] <= 4) {
        return nil;
    }
    NSUInteger countOfCharToReplace = [aString length] - 4;
    NSString *firstPart = @"*";
    while(--countOfCharToReplace) {
        firstPart = [firstPart stringByAppendingString:@"*"];
    }
    // range for the last four
    NSRange lastFourRange = NSMakeRange([aString length] - 4, 4);
    // return the combined string
    return [firstPart stringByAppendingString:
            [aString substringWithRange:lastFourRange]];
}

可可中最不可靠的部分正在制造重复的恒星,没有某种宽松。 。 允许你重复你喜欢的一段时期,这样一来,你就能够在此简单地解决问题:

NSInteger starUpTo = [string length] - 4;
if (starUpTo > 0) {
    NSString *stars = [@"" stringByPaddingToLength:starUpTo withString:@"*" startingAtIndex:0];
    return [string stringByReplacingCharactersInRange:NSMakeRange(0, starUpTo) withString:stars];
} else {
    return string;
}

我不敢肯定为什么接受所接受的答复,因为只有一切,最后4个只是一位数。 这里简单:

NSMutableString * str1 = [[NSMutableString alloc]initWithString:@"1234567890ABCD"];
NSRange r = NSMakeRange(0, [str1 length] - 4);
[str1 replaceCharactersInRange:r withString:[[NSString string] stringByPaddingToLength:r.length withString:@"*" startingAtIndex:0]];
NSLog(@"%@",str1);

您可使用<代码>[String substring ToIndex:[String length]-4],以获得座右的第一部分,然后将[String length]-4 *与第二部分合并。 也许,它们更容易做到这一点。

NSMutableString * str1 = [[NSMutableString alloc]initWithString:@"1234567890ABCD"];
[str1 replaceCharactersInRange:NSMakeRange(0, [str1 length] - 4) withString:@"*"];
NSLog(@"%@",str1);

它的工作

The regexp don t work on Ri7, but may this helps:

- (NSString *)encryptString:(NSString *)pass {

    NSMutableString *secret = [NSMutableString new];

    for (int i=0; i<[pass length]; i++) {
        [secret appendString:@"*"];
    }

    return secret;
}

在你的情况下,你应停止更换最后4个特性。 做比特工,但获得工作





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签