Removing Parentheses from The String in iOS

Removing parentheses from the string in iOS

Try using

[[attributedLabel.text stringByReplacingOccurrencesOfString:@"(" withString:@""] stringByReplacingOccurrencesOfString:@")" withString:@""];
instead.

Remove substring between parentheses

Most simple Shortest solution is regular expression:

let string = "This () is a test string (with parentheses)"
let trimmedString = string.replacingOccurrences(of: "\\s?\\([\\w\\s]*\\)", with: "", options: .regularExpression)

The pattern searches for:

  • An optional whitespace character \\s?.
  • An opening parenthesis \\(.
  • Zero or more word or whitespace characters [\\w\\s]*.
  • A closing parenthesis \\).

Alternative pattern is "\\s?\\([^)]*\\)" which represents:

  • An optional whitespace character \\s?.
  • An opening parenthesis \\(.
  • Zero or more characters anything but a closing parenthesis [^)]*.
  • A closing parenthesis \\).

Swift regex format to remove string contains brackets in iOS

Use replacingOccurrences(…)

let result = str.replacingOccurrences(of: #"\(.*\)"#, 
with: "",
options: .regularExpression)
.trimmingCharacters(in: .whitespaces))

Remove parentheses from array value converted to string

Use join:

self.txtpstn.text =  "\((self.no_pstn.self as NSArray).componentsJoined(by: ","))"
self.txtmobile.text = "\(self.no_mobile.self as NSArray).componentsJoined(by: ","))"

How to remove all values within square brackets, including the brackets | Swift

you could try something simple like this:

var sampleString = "[2049A30-3930Q4] The Rest of the String"

sampleString.removeSubrange(sampleString.startIndex..."[2049A30-3930Q4]".endIndex)
// this will also work
// sampleString.removeSubrange(sampleString.startIndex..."[0000000-000000]".endIndex)
print("----> sampleString: \(sampleString)")


EDIT-1: more general approach if needed.

if let from = sampleString.range(of: "[")?.lowerBound,
let to = sampleString.range(of: "]")?.upperBound {
sampleString.removeSubrange(from...to)
print("----> sampleString: \(sampleString)")
}

How to remove double quotes and brackets from NSString

Use following code:

NSCharacterSet *unwantedChars = [NSCharacterSet characterSetWithCharactersInString:@"\"[]"];
NSString *requiredString = [[_finalIDStr componentsSeparatedByCharactersInSet:unwantedChars] componentsJoinedByString: @""];

It's efficient and effective way to remove as many characters from your string in one single line..

Removing brackets from UILabel in iOS

Try to change the last two line with this two:

[attributedLabel setText:[[attributedLabel.text stringByReplacingOccurrencesOfString:@"[" withString:@""] stringByReplacingOccurrencesOfString:@"]" withString:@""]];
return attributedLabel;

The methods that begins with string... does not change the string itself only returns a new string that is changed.

By the way, NSString objects are immutable. If you want to change strings you can use NSMutableString, the below implementation use only the NSMutabeString, that you are already using in the block.

-

Try This:

-(TTTAttributedLabel*)setItalicTextForLabel:(TTTAttributedLabel*)attributedLabel fontSize:(float)Size
{
[attributedLabel setText:[self.infoDictionary objectForKey:@"description"] afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString)
{
NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]);
NSRegularExpression *regexp = ParenthesisRegularExpression();
UIFont *italicSystemFont = [UIFont italicSystemFontOfSize:Size];
DLog(@"%@",italicSystemFont.fontName);
CTFontRef italicFont = CTFontCreateWithName((__bridge CFStringRef)italicSystemFont.fontName, italicSystemFont.pointSize, NULL);
[regexp enumerateMatchesInString:[mutableAttributedString string] options:0 range:stringRange usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
if (italicFont) {
[mutableAttributedString removeAttribute:(NSString *)kCTFontAttributeName range:result.range];
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)italicFont range:result.range];
CFRelease(italicFont);
NSRange range1 = NSMakeRange (result.range.location, 1);
NSRange range2 = NSMakeRange (result.range.location + result.range.length-2, 1);
[mutableAttributedString replaceCharactersInRange:range1 withString:@""];
[mutableAttributedString replaceCharactersInRange:range2 withString:@""];
}
}];
return mutableAttributedString;
}];
return attributedLabel;
}

how to remove outer ( ) from string contain multiple ( ) in swift

If you are sure you will always have outer "()" that are part of the string itself you can achieve that in multiple ways, one way is:

var myString = "(A-01: FLURO ENGINEERING P LTD.(HIWIN))"

myString = String(myString.dropFirst())
myString = String(myString.dropLast())

print(myString)

Which prints out:

A-01: FLURO ENGINEERING P LTD.(HIWIN)


If you are not sure, but would like to remove outer "()" in case they are both present, you can simply, as one solution just check it like this before dropping first and last character:

if myString.first == "(", myString.last == ")" {
myString = String(myString.dropFirst())
myString = String(myString.dropLast())
}


Related Topics



Leave a reply



Submit