问题标签 [stringwithformat]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
6 回答
1254 浏览

objective-c - 如何检查 NSString 格式是否包含与可变参数相同数量的说明符?

为了确保返回的格式化字符串NSString initWithFormat:arguments:符合预期,我需要确定格式说明符的数量是否与参数相同。下面是一个(略微做作和高度编辑的)示例:

使用此方法,[self thingsForStuff:CustomStuffTwo, @"Hello", @"World"]将记录

“两件事:你好和世界”

...但[self thingsForStuff:CustomStuffTwo, @"Hello"]会记录

“两件事:你好和”

......在它发生之前最好被抓住的东西。

有没有办法计算字符串中的格式说明符,最好是轻量级/便宜的?

0 投票
4 回答
1507 浏览

iphone - iPhone:浮点数后应该有多少位数字

我正在尝试使用分段控件设置浮点数的位数。

所以我用“0”、“1”、“2”和“3”创建了一个分段控件。我想用变量(self._segmentedControl.selectedSegmentIndex)设置逗号后的数字。

我知道我可以决定逗号后的位数应该是这样的:

有人可以帮帮我吗?

0 投票
3 回答
1192 浏览

iphone - NSString vs %i with stringWithFormat

Ok I am not sure how or why this is working but if someone could explain this to me it would be greatly appreciated.

I am debugging an iPhone app and Xcode gives me the following warning, which is correct: "Format Specifies type 'int' but the Argument has a Type 'NSString'.

The code below returns a number, but if I set it to a string it returns a very long string. I know that they wanted the integer and not the string, but I do not know why this would return a number for a string because when I do change it using [str intValue] I get "0" not the long number. So how is this working, can someone explain this to me?

Here is the code:


Ok let me show you what I mean, and don't worry about the string being passed please. I just want to know how it produces a number like what is shown below from it. I did three NSLogs:

Results:

Anyone know why this happens?

Thank You!

0 投票
3 回答
642 浏览

iphone - StringWithFormat 返回非 NSString

我有StringWithFormat不工作的问题。调试器说返回的字符串不是一个NSString(并且它不是空的),因此我的调用应该NSString与方法选择器不匹配,我的应用程序崩溃了。

我正在测试 XCode 4.5 iOS 6 设置(旧项目,没有子项目)。

此代码将返回不是NSString

如果我在格式字符串中插入一个空格作为第一个字符,则结果为NSString,即以下工作:

此代码块也不起作用。它返回的东西不是NSString. 我在这里做错了什么?

错误信息是ttl = (__NSCFString *) 0xwhatever @<variable is not NSString>

0 投票
2 回答
1384 浏览

ios - UITextView 删除最后输入的文本

我有一个 UITextView 由 IBAction 上的几个不同 UITextFields 传递文本。

我正在尝试从 TextView 中删除选定的文本

但它会删除所有文本,而不仅仅是选定的文本。

请有人给我一些指导,谢谢。

0 投票
1 回答
831 浏览

objective-c - 将 NSInteger 转换为 NSString 作为 5 位数字

我有一个 int 并将其转换为 NSString ,如下所示:

myInt 的范围从 0 到 99999。我想要的是将 myStr 作为 5 位数字。

例如:00072 或 09856 或 00002

谢谢你。

0 投票
1 回答
79 浏览

xcode - 需要语法帮助(使用字符串变量)

我正在做一些轻微或严重错误的事情。

我想将 UIColour 换成可编程变量,例如:

我在 NSString 部分收到一个 Expected Identifier 错误,并且没有制定完成此操作所需的格式。

0 投票
2 回答
530 浏览

ios - NSString stringWithFormat 返回类型,为什么是“id”?

为什么该方法[NSString stringWithFormat]返回一个id类型?从我期望的名称来看,它返回一个NSString,而不是通用指针。其他类遵循这个“规则”。例如[NSNumber numberWithInt]返回一个NSNumber,而不是一个id

我认为从工厂方法之类的事实来看,这甚至是不合理的。

0 投票
3 回答
9957 浏览

objective-c - 使用 NSString stringWithFormat 的动态字符串格式

我一直在尝试制作动态字符串格式,以便通过用户选项精度或填充可以在某种程度上由用户定义。

一个示例包括以 24 小时时间格式填充前导零。在正常时间格式中,小时可以用单个数字表示,也可以用零填充的数字表示。这以字符串格式表示:

我想要实现的是有一个包含@""or的变量,并将该变量显示在和@"02"之间的格式字符串中。%i

我已经做了几个实验,但似乎无法做到这一点,并且开始认为这是不可能的stringWithFormat

我试过了:

...stringWithFormat:@"Hour: %%@i", padding, hour

...stringWithFormat:@"Hour: %@%i", padding, hour

和别的。

有任何想法吗?

0 投票
3 回答
1534 浏览

c# - 将目标 c 方法转换为 c# stringWithFormat:@"%@%.0f%@"

好的,我正在努力将一些客观的 c 代码转换为 c#,这就是我所拥有的。

所以我创建了一个辅助类,我有这个:

所以,我的问题是“%.0f”和“dataUsingEncoding:NSTimeInterval”。我知道第一部分与格式化“now”参数有关,我需要做些什么来确保我在 c# 中做同样的事情,有人可以向我详细解释或指导我我应该阅读的文章/博客?

谢谢!

更新:好吧,伙计们,我搞砸了,很抱歉我复制和粘贴错误,这是我困惑的一方。dataUsingEncoding:NSTimeInterval 应为:dataUsingEncoding:NSUTF8StringEncoding。所以我已经修复了这个帖子。