如何从返回字符串中删除新行


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

我有一个简单的PHP脚本,只返回字符串"hi"代码如下:

<?php include "base.php"?>
<?php
echo "hi";
?>

在xCode中,我得到以下内容:

2014-12-15

10:56:24.048 MyApp[12515:1603] resultString =

你好

出于某种原因,它会在字符串之前添加一个新行。如何防止生成此新行?这是我编码字符串的方式吗?这是我的字符串

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

这样它就可以工作!!

<?php
include "base.php"
echo "hi";
?>

不能在 Objective C 方面提供太多帮助,但是您的 php 文件中是否碰巧有一个杂散的换行符?请记住,<?php ?>标签之外的任何内容都在 PHP 中按字面解释。尝试删除结束?> - 这实际上是纯代码 PHP 文件的首选,因为代码的结尾意味着关闭代码块,并且可以防止尾随换行符影响任何内容。

你可以

这样尝试:-

//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);