如何从nsstring中删除前3个字符?

时间:2021-09-27 22:16:15

I have a string like this "A. rahul VyAs"

我有一条像这样的线" a。rahul VyAs"

and i want to remove "A. " and the space after the "A." so that new string would be "rahul VyAs"

我想去掉A。" a "后面的空格这样新的字符串就会是"rahul VyAs"

How do i achieve this?

我该如何做到这一点?

4 个解决方案

#1


236  

You can use the NSString instance methods substringWithRange: or substringFromIndex:

您可以使用NSString实例方法substringWithRange:或substringFromIndex:

NSString *str = @"A. rahul VyAs";
NSString *newStr = [str substringWithRange:NSMakeRange(3, [str length]-3)];

or

NSString *str = @"A. rahul VyAs";
NSString *newStr = [str substringFromIndex:3];

#2


2  

Try this,

试试这个,

char *string=[@"A. rahul VyAs" cStringUsingEncoding:NSUTF8StringEncoding];
char *subString=&name[3];
NSString *newString=[NSString stringWithCString:subString encoding:NSUTF8StringEncoding];

#3


1  

This is a solution I have seen specifically for removing regularly occurring prefixes and solving the answer to the question How do I remove "A. "?

这是我特别看到的一个解决方案,用于删除经常出现的前缀,并解决如何删除“a”的问题。”?

NSString * name =  @"A. rahul VyAs";
NSString * prefixToRemove = @"A. "; 
name = [name stringByReplacingOccurrencesOfString:prefixToRemove withString:@""];

This code will remove what you tell it to remove/change if the character set exists, such as "A. ", even if the three characters (or more/less) are in the middle of the string.

如果字符集存在,该代码将删除您告诉它要删除/更改的内容,例如“A”。,即使三个字符(或多或少)位于字符串的中间。

If you wanted to remove rahul, you can. It's diverse in that you specify exactly what you want removed or changed, and if it exists anywhere in the String, it will be removed or changed.

如果你想除掉拉胡尔,你可以。它的不同之处在于,您指定了您想要删除或更改的内容,如果它存在于字符串中的任何位置,它将被删除或更改。

If you only want a certain specified number of characters removed from the front of the text that are always random or unknown, use the [string length] method as is the top answer.

如果您只希望从文本前面删除一定数量的字符,而这些字符总是随机的或未知的,请使用[string length]方法作为最上面的答案。

If you want to remove or change certain characters that repeatedly appear, the method I have used will enable that, similar to Wordsearch on document editors.

如果您想删除或更改重复出现的某些字符,那么我使用的方法将启用这些字符,类似于文档编辑器上的Wordsearch。

#4


-1  

It's this simple:

这个简单的:

myString = [myString subStringFromIndex:3]

That's it.

就是这样。

#1


236  

You can use the NSString instance methods substringWithRange: or substringFromIndex:

您可以使用NSString实例方法substringWithRange:或substringFromIndex:

NSString *str = @"A. rahul VyAs";
NSString *newStr = [str substringWithRange:NSMakeRange(3, [str length]-3)];

or

NSString *str = @"A. rahul VyAs";
NSString *newStr = [str substringFromIndex:3];

#2


2  

Try this,

试试这个,

char *string=[@"A. rahul VyAs" cStringUsingEncoding:NSUTF8StringEncoding];
char *subString=&name[3];
NSString *newString=[NSString stringWithCString:subString encoding:NSUTF8StringEncoding];

#3


1  

This is a solution I have seen specifically for removing regularly occurring prefixes and solving the answer to the question How do I remove "A. "?

这是我特别看到的一个解决方案,用于删除经常出现的前缀,并解决如何删除“a”的问题。”?

NSString * name =  @"A. rahul VyAs";
NSString * prefixToRemove = @"A. "; 
name = [name stringByReplacingOccurrencesOfString:prefixToRemove withString:@""];

This code will remove what you tell it to remove/change if the character set exists, such as "A. ", even if the three characters (or more/less) are in the middle of the string.

如果字符集存在,该代码将删除您告诉它要删除/更改的内容,例如“A”。,即使三个字符(或多或少)位于字符串的中间。

If you wanted to remove rahul, you can. It's diverse in that you specify exactly what you want removed or changed, and if it exists anywhere in the String, it will be removed or changed.

如果你想除掉拉胡尔,你可以。它的不同之处在于,您指定了您想要删除或更改的内容,如果它存在于字符串中的任何位置,它将被删除或更改。

If you only want a certain specified number of characters removed from the front of the text that are always random or unknown, use the [string length] method as is the top answer.

如果您只希望从文本前面删除一定数量的字符,而这些字符总是随机的或未知的,请使用[string length]方法作为最上面的答案。

If you want to remove or change certain characters that repeatedly appear, the method I have used will enable that, similar to Wordsearch on document editors.

如果您想删除或更改重复出现的某些字符,那么我使用的方法将启用这些字符,类似于文档编辑器上的Wordsearch。

#4


-1  

It's this simple:

这个简单的:

myString = [myString subStringFromIndex:3]

That's it.

就是这样。