Questions tagged «string»

字符串是有限的符号序列,通常用于文本,尽管有时用于任意数据。


10
在引号内使用引号
当我想print在Python中执行命令并且需要使用引号时,我不知道如何在不关闭字符串的情况下执行该命令。 例如: print " "a word that needs quotation marks" " 但是,当我尝试执行上面的操作时,我最终关闭了字符串,并且无法将需要的单词放在引号之间。 我怎样才能做到这一点?

7
如何在字符串中写反斜杠(\)?
我想写这样的事情C:\Users\UserName\Documents\Tasks在textbox: txtPath.Text = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+"\Tasks"; 我得到错误: 无法识别的转义序列。 如何在字符串中写反斜杠?
72 c#  string  winforms 

6
Python:Pandas根据字符串长度过滤字符串数据
我喜欢过滤掉字符串长度不等于10的数据。 如果我尝试过滤掉列A或B的字符串长度不等于10的任何行,则尝试这样做。 df=pd.read_csv('filex.csv') df.A=df.A.apply(lambda x: x if len(x)== 10 else np.nan) df.B=df.B.apply(lambda x: x if len(x)== 10 else np.nan) df=df.dropna(subset=['A','B'], how='any') 这工作缓慢,但正在工作。 但是,当A中的数据不是字符串而是数字(在read_csv读取输入文件时解释为数字)时,有时会产生错误。 File "<stdin>", line 1, in <lambda> TypeError: object of type 'float' has no len() 我相信应该有更高效,更优雅的代码来代替。 根据下面的答案和评论,我找到的最简单的解决方案是: df=df[df.A.apply(lambda x: len(str(x))==10] df=df[df.B.apply(lambda x: len(str(x))==10] 要么 df=df[(df.A.apply(lambda x: len(str(x))==10) & …
72 python  string  pandas  csv  filter 


7
如何在Python中检查字符是否为大写?
我有这样的字符串 >>> x="Alpha_beta_Gamma" >>> words = [y for y in x.split('_')] >>> words ['Alpha', 'beta', 'Gamma'] 我想要输出说X不符合要求,因为列表单词的第二个元素以小写开头,如果字符串x = "Alpha_Beta_Gamma"则应打印符合要求的字符串
71 python  string 


1
Swift 3不正确的字符串插值,带有隐式展开的Optionals
为什么隐式解开了可选在Swift 3中使用字符串插值时,没有解开变量? 示例:在操场上运行以下代码 var str: String! str = "Hello" print("The following should not be printed as an optional: \(str)") 产生以下输出: The following should not be printed as an optional: Optional("Hello") 当然,我可以将字符串与 +运算符将字符串但是我在应用程序中的几乎所有地方都使用了字符串插值,由于这个原因,现在不再起作用了(错误?)。 这甚至是一个错误,还是他们有意使用Swift 3更改了此行为?


7
Python:使用“ ..%(var)s ..”%locals()是一种好习惯吗?
我发现了这种模式(或反模式),对此我感到非常满意。 我觉得它非常敏捷: def example(): age = ... name = ... print "hello %(name)s you are %(age)s years old" % locals() 有时我用它的表弟: def example2(obj): print "The file at %(path)s has %(length)s bytes" % obj.__dict__ 我不需要创建人为的元组并计算参数并将%s匹配位置保留在元组中。 你喜欢它吗?您会/会使用它吗?是/否,请解释。


5
为什么我得到的字符串没有命名为Error?
game.cpp #include <iostream> #include <string> #include <sstream> #include "game.h" #include "board.h" #include "piece.h" using namespace std; 游戏 #ifndef GAME_H #define GAME_H #include <string> class Game { private: string white; string black; string title; public: Game(istream&, ostream&); void display(colour, short); }; #endif 错误是: game.h:8 error: 'string' does not name a type …
71 c++  string  std 

4
查找两个字符串之间不同的第一个字符
给定两个相等长度的字符串,是否有一种优雅的方法来获取第一个不同字符的偏移量? 显而易见的解决方案是: for ($offset = 0; $offset < $length; ++$offset) { if ($str1[$offset] !== $str2[$offset]) { return $offset; } } 但这对于如此简单的任务而言似乎并不正确。
71 php  string 

12
php字符串串联,性能
在Java和C#之类的语言中,字符串是不可变的,并且一次构建一个字符的字符串在计算上会非常昂贵。在上述语言中,有一些库类可以降低这种成本,例如C#System.Text.StringBuilder和Javajava.lang.StringBuilder。 php(4或5;我对两者都感兴趣)是否都共享此限制?如果是这样,是否有类似的解决方案?

10
“字符串”定义背后的历史
直到最近我才想过,但是我不确定为什么我们称呼string strings。我是.NET程序员,但是我相信字符串的概念几乎存在于每种编程语言中。 在编程之外,我不相信我曾听说过string用于描述单词或字母的单词。快速搜索“定义:字符串”会产生一堆定义,这些定义与字母,单词或与编程相关的任何性质无关。 我的猜测是,在过去,字符串实际上只是特定长度的字符数组,通常在结尾处带有定界字符。但是,我看不到从“字符数组”到的自然过渡string。 有人可以提供一些我们为什么称呼字符串的见解strings吗?

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.