Unknown escape sequence '\x20' - ios

I've created this code
NSString *insertSQL = [NSString stringWithFormat:#"Update Expense_Group set Expense_sum = Expense_sum-\%#\ where Expense_Type_Id = \"%#\"",strExpAmount,current_Expense_TypeId];
and now,i got this warning "Unknown escape sequence '\x20'".

You only need to "\" escape the quotes within that "stringWithFormat:" call...

NSString *insertSQL = [NSString stringWithFormat:#"Update Expense_Group set Expense_sum = Expense_sum-%# where Expense_Type_Id = %#",strExpAmount,current_Expense_TypeId];
This may solve the issue no need of escape sequence before %#.

NSString *insertSQL = [NSString stringWithFormat:#"Update Expense_Group set Expense_sum = Expense_sum-'%#' where Expense_Type_Id = '%#'",strExpAmount,current_Expense_TypeId];

Related

Concatenate two strings with a backslash iOS

I am new to Objective-C and I need to have a string like "abc\123"
To have this I have tried doing:-
NSString *first = #"abc\\"; //Should escape
NSString *second= #"123";
NSString *combined= [NSString stringWithFormat:#"%#%#", first, second]; //which should give abc\123
But I get an output as "abc\\123".
I am really stuck on this one. Any help is appreciated
Backslash itself is the escape character so needs character to read ahead, you need to escape it.
This results same output you like.
NSString *first = #"abc\\";
You can check just by adding one more backshash #"abc\\\" gives you missing character "" runtime error.
NSString *first = #"abc\\"; // log results abc\
NSString *first = #"abc\\\\"; // log results abc\\
Can do by adding between format specifiers by following same backslash rule.
NSString *combined= [NSString stringWithFormat:#"%#\\%#", first, second];
It's the output issue, in fact, the string is correct.
let array = ["abc", "123"]
let separator = "\\"
separator.characters.count //count=1
let x = array.joined(separator: "\\")
x.characters.count //count = 7
You can use
stringByReplacingOccurrencesOfString:withString:
So in your case
NSString *first = #"abc\\"; //Should escape
NSString *second= #"123";
NSString *first = [first stringByReplacingOccurrencesOfString:#"\\" withString:#"\"];
NSString *combined= [NSString stringWithFormat:#"%#%#", first, second];
So your result will be abc\123

iOS: Create String from Variable and string combined

I need to concatenate a string and a variable together - I kind of find lots of examples of adding a string prior to a variable but not the other way round - how do I do this?
NSString *theImage = [[self.detailItem valueForKey:#" resimagefiletitle"] description], #"'add on the end";
Something like this:
NSString *theImage = [NSString stringWithFormat:#"%# %#",[self.detailItem valueForKey:#" resimagefiletitle"], #"'add on the end"];
Or:
NSString *theImage = [NSString stringWithFormat:#"%# add on the end",[self.detailItem valueForKey:#" resimagefiletitle"]];
Try this
NSString *theImage = [NSString stringWithFormat:#"%# '>",[[self.detailItem valueForKey:#"resimagefiletitle"] description]];
Here I am considering [[self.detailItem valueForKey:#"resimagefiletitle"] description] gives NSString
We can concat diffrent type of datatypes into string by mention the format for it.
like if your want to concat two or more strings together then you can use the following code:
NSString *NewString = [NSString stringWithFormat:#"%#%#",#"This Is",#"way to concate string"];
and if your want concat integer value then you can mention the data format for it "%i".
eg:
int OutOf = 150;
NSString *NewString = [NSString stringWithFormat:#"%#%i",#"I got 100 out of ",OutOf];
this may help you.

Removing last characters of NSString until it hits a separator

I've got a string that shows the stock amount using "-" as separators.
It's built up like this: localStock-wareHouseStock-supplierStock
Now I want to update the supplierStock at the end of the string, but as you can see in the code below it goes wrong when the original string returns more than a single-space value (such as 20).
Is there a way to remove all characters until the last "-" (or remove characters after the second "-")?
NSMutableString *string1 = [NSMutableString stringWithString: p1.colorStock];
NSLog(#"string1: %#",string1);
NSString *newString = [string1 substringToIndex:[string1 length]-2];
NSLog(#"newString: %#",newString);
NSString *colorStock = [NSString stringWithFormat:#"%#-%#",newString,p2.supplierStock];
NSLog(#"colorstock: %#",colorStock);
p1.colorStock = colorStock;
NSLog1
string1: 0-0-0
newString: 0-0
colorstock: 0-0-20
NSLog2
string1: 0-0-20
newString: 0-0-
colorstock: 0-0--20
EDIT: Got it working thanks to Srikar!
NSString *string1 = [NSString stringWithString: p1.colorStock];
NSLog(#"string1: %#",string1);
NSString *finalString = [string1 stringByReplacingOccurrencesOfString:[[string1 componentsSeparatedByString:#"-"] lastObject] withString:p2.supplierStock.stringValue];
NSLog(#"finalString: %#",finalString);
p1.colorStock = finalString;
Why not use componentsSeparatedByString followed by lastObject ?
NSString *supplierStock = [[string1 componentsSeparatedByString:#"-"] lastObject];
The above works if the "stock amount" is always in sets of 3's separated by a "-". Also since you always want supplierStock, lastObject is perfect for your needs.
Of course after splitting string1 with - you get a NSArray instance and you can access the individual components using objectAtIndex:index. So if you want localStock you can get by
NSString *localStock = [[string1 componentsSeparatedByString:#"-"] objectAtIndex:0];
I would suggest splitting the string into the 3 parts using [NSString componentsSeparatedByString:#"-"] and then building it back up again:
NSArray *components = [p1.colorStock componentsSeparatedByString:#"-"];
p1.colorStock = [NSString stringWithFormat:#"%#-%#-%#",
[components objectAtIndex:0],
[components objectAtIndex:1],
p2.supplierStock];
With a string that looks like
NSString *myString = #"Hello-World";
you can separate it with the componentsSeparatedByString: method of the NSString object as
NSArray *myWords = [myString componentsSeparatedByString:#"-"];
The myWords - array will then contain the two NSString objects Hello and World.
To access the strings:
NSString *theHelloString = [myWords objectAtIndex:0];
NSString *theWorldString = [myWords objectAtIndex:1];
Hope it helps!
None of these examples show how to do this if you are unaware of how many of these separator occurrences you're going to have in the original string.
Here's what I believe the correct the correct code should be for dismantling the original string and rebuilding it until you reach the final separator, regardless of how many separators it contains.
NSString *seperator = #" ";
NSString *everythingBeforeLastSeperator;
NSArray *stringComponents = [originalString componentsSeparatedByString:seperator];
if (stringComponents.count!=0) {
everythingBeforeLastSeperator = [stringComponents objectAtIndex:0];
for (int a = 1 ; a < (stringComponents.count - 1) ; a++) {
everythingBeforeLastSeperator = [NSString stringWithFormat:#"%#%#%#", everythingBeforeLastSeperator, seperator, [stringComponents objectAtIndex:a]];
}
}
return everythingBeforeLastSeperator;

How to right pad a string using stringWithFormat

I would like to be able to right align a string using spaces. I have to be able to use the stringWithFormat: method.
So far I have tried the recommended format and it does not seem to work: [NSString stringWithFormat:#"%10#",#"test"].
I would expect this to return a string that has six spaces followed by "test" but all I am getting is "test" with no spaces.
It appears that stringWithFormat ignores the sizing requests of the %# format specifier. However, %s specifier works correctly:
NSString *test = #"test";
NSString *str = [NSString stringWithFormat:#"%10s", [test cStringUsingEncoding:NSASCIIStringEncoding]];
NSLog(#"'%#'", str);
This prints ' test'.
It's C style formatting. %nd means the width is n.
check following code.
NSLog(#"%10#",[NSString stringWithFormat:#"%10#",#"test"]);
NSLog(#"%#",[NSString stringWithFormat:#" %#",#"test"]);
NSLog(#"%10#", #"test");
NSLog(#"%10s", [#"test" cStringUsingEncoding:[NSString defaultCStringEncoding]]);
NSLog(#"%10d", 1);
NSString *str = #"test";
int padding = 10-[str length]; //6
if (padding > 0)
{
NSString *pad = [[NSString string] stringByPaddingToLength:padding withString:#" " startingAtIndex:0];
str = [pad stringByAppendingString:str];
}
NSLog(#"%#", str);

Inserting a string into another string

I am working on iPhone app. I have the SQL query:
NSString *myRed = [NSString stringWithFormat: #"%1.4f", slideR.value];
[self insertData:#"INSERT INTO colour VALUES("+myRed+")"];
It produces syntax error. How to insert a string into string. That approach in Java would have worked.
Best regards
Try this :
NSString *myRed = [NSString stringWithFormat: #"%1.4f", slideR.value];
[self insertData:[NSString stringWithFormat:#"INSERT INTO colour VALUES(\"%#\")",myRed]];
If you don't want " then :-
[self insertData:[NSString stringWithFormat:#"INSERT INTO colour VALUES(%#)",myRed]];
Hope it helps you.
You could try writing your query and your string appended together before hand.
NSString *str = #"Your values";
NSString *query = [NSString stringWithFormat:#"INSERT INTO color VALUES(%#)", str];
[self insertData:query];

Resources