Jayasabeen Appukuttan
Jayasabeen Appukuttan

Reputation: 1440

Add blank space at end of NSString

I am using NSString class to display a string with a format like ' STRING ' a empty space should display in begining and end of string

My code is

NSMutableString *MUT_STR = [NSMutableString stringWithString:@" TEST"];
    [MUT_STR insertString:@" " atIndex: MUT_STR.length];

I tried like the following code also

NSString *STR_test = @" TEST ";

At present my output is like ' TEST'

Thanks

Upvotes: 1

Views: 2416

Answers (6)

Ajith Tom
Ajith Tom

Reputation: 31

The code looks good for the expected result.

Case 1

NSMutableString *MUT_STR = [NSMutableString stringWithString:@" TEST"];
[MUT_STR insertString:@" " atIndex: MUT_STR.length];
NSLog(@"output : |%@|",MUT_STR);
self.lbl.text = MUT_STR;

With trailing and leading spaces

Case 2

NSMutableString *MUT_STR = [NSMutableString stringWithString:@"TEST"];
[MUT_STR insertString:@" " atIndex: MUT_STR.length];
NSLog(@"output : |%@|",MUT_STR);
self.lbl.text = MUT_STR;

Case 3

NSMutableString *MUT_STR = [NSMutableString stringWithString:@" TEST"];
//[MUT_STR insertString:@" " atIndex: MUT_STR.length];
NSLog(@"output : |%@|",MUT_STR);
self.lbl.text = MUT_STR;

Case 4 attributed text

NSMutableString *MUT_STR = [NSMutableString stringWithString:@" TEST"];
[MUT_STR insertString:@" " atIndex: MUT_STR.length];
NSLog(@"output : |%@|",MUT_STR);
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor blueColor] forKey:NSBackgroundColorAttributeName];

NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:MUT_STR attributes:attributes];

//self.lbl.text = MUT_STR;
self.lbl.attributedText = attStr;

blue area is where there is text content

Make sure you are not trimming out trailing spaces or not replacing blank spaces anywhere within the code flow

Upvotes: 0

Nirmalsinh Rathod
Nirmalsinh Rathod

Reputation: 5186

Hey I tried with what you do, its give perfect output at my end. Check the attached screen shots.

enter image description here

If you still having issue then you can use below code:

    NSString *STR_test = @" TEST";
    NSString *result = [NSString stringWithFormat:@"%@ ",STR_test];
NSLog(@"%@",result);

This is what I tried:

NSString *STR_test = @" TEST"; // NSLog(@"%@",STR_test);

NSString *result = [NSString stringWithFormat:@"%@ ",STR_test];
NSLog(@"%@",result);

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 50)];
label.text = [NSString stringWithFormat:@"%@",result];
label.backgroundColor = [UIColor redColor];
[label sizeToFit];
[self.view addSubview:label];

OUTPUT: enter image description here

Upvotes: 0

Rohan Dave
Rohan Dave

Reputation: 261

Swift

    let str = "Hello"
    var finalStr: String = " \(str)"
    let result = finalStr.padding(toLength: max((finalStr.characters.count) + 1, (str.characters.count)), withPad: " ", startingAt: 0)
    print("Result:\'\(result)\'")

Upvotes: 0

Sakir Sherasiya
Sakir Sherasiya

Reputation: 1572

Try as below:

NSString *MUT_STR = @" TEST";
MUT_STR = [MUT_STR stringByAppendingString:@" "];
NSLog(@"%@",MUT_STR);

Upvotes: 0

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

you can do this in multiple ways.

for e.g

// add the space in in front of string
NSString *finalString = [NSString stringWithFormat:@" %@",STR_test];
// calculate the string length and append the space in end of the string
NSString *result = [finalString stringByPaddingToLength:MAX(finalString.length + 1, STR_test.length)
                                      withString:@" " startingAtIndex:0];
NSLog(@"final Result:\'%@\'", result);

another simple option

// add space in start and end of the string

NSString *finalString = [NSString stringWithFormat:@" %@ ",STR_test];

Upvotes: 2

Subbu
Subbu

Reputation: 21

I have tried this and it worked for me

NSString *reqString=[NSString stringWithFormat:@"%@%@%@",@"",@"YourString",@""]; NSLog(@"%@",reqString);

Pass the string in place of your string.

Upvotes: 0

Related Questions