简体   繁体   中英

How do i get rid of new line from a return string?

I have a simple PHP script that just returns the string "hi" Here's the code:

<?php include "base.php"?>

<?php
echo "hi";
?>

In xCode I get the following:

2014-12-15 10:56:24.048 MyApp[12515:1603] resultString =

hi

For some reason it adds a new line before the string. How can I prevent this new line from being generated? Is it the way I'm encoding the string? Here's my string

NSString *resultsString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

THIS WAY IT WORKS!!

<?php
include "base.php"

echo "hi";
?>

Can't offer much help with the Objective C side of things, but does there happen to be a stray newline in your php file? Remember, anything outside of the <?php ?> tags is interpreted literally in PHP. Try removing the closing ?> - this is actually preferred for pure-code PHP files, since the end of the code implies closing the code block and it prevents trailing newlines from affecting anything.

You can try like this:-

//Assuming below string after reading from PHP
 NSString *resultsString = @"\nhi";

//So try below api to replace new line
    NSString *modifiedString = [resultsString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    NSLog(@"%@",modifiedString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM