如何仅从字符串的开头删除点

时间:2017-11-30 12:02:00

标签: ios objective-c nsstring

我的字符串文字是赞 -

1) .....bla ..bla...
2)...bla.. bla…bla.
3).bla.. bla…bla.

点不是静态的。我想仅从开始时删除点。不是所有点

我试过这个

NSString *newString = [myString stringByReplacingOccurrencesOfString:@"." withString:@""];

但这是删除所有点。

4 个答案:

答案 0 :(得分:3)

试试这个。

NSString* regex = @"^\\.*";
NSString* input = @"....abc..z";
NSString* output = [input stringByReplacingOccurrencesOfString:regex withString:@"" options:NSRegularExpressionSearch range: [input rangeOfString:input]];

答案 1 :(得分:1)

var test = "...abc";

while test.hasPrefix(".") {
    test.remove(at: test.startIndex)
}
//test variable will have dots removed at the start.

试试这个。

答案 2 :(得分:0)

试试这个:

NSString *str = @"...Videt...IDL";

__block NSInteger loc = 0;
[str enumerateSubstringsInRange:NSMakeRange(0, str.length) options:NSStringEnumerationByWords usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
    loc = substringRange.location;
    *stop = YES;
}];

NSString *convStr = [str substringFromIndex:loc];
NSLog(@"%@", convStr);

答案 3 :(得分:0)

NSString *str = @"....bla .... bla..... bla";
    NSCharacterSet *charSet = [NSCharacterSet letterCharacterSet];
    int letterNO = 0;
    for (int i =0; i<str.length; i++)
    {
        NSString *letter = [NSString stringWithFormat:@"%c",[str characterAtIndex:i]];
        if ([letter rangeOfCharacterFromSet:charSet].location != NSNotFound)
        {
            letterNO = i;
            break;
        }
    }
    NSString * newString = [str substringFromIndex:letterNO];
    NSLog(@"newString %@",newString);