Questions tagged «string»

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

8
如何在javascript中每n个字符后插入一个字符?
我有一个字串: “敏捷的棕色狐狸跳过了懒狗。” 我想使用JavaScript(可能是jQuery)每n个字符插入一个字符。例如,我想打电话: var s = "The quick brown fox jumps over the lazy dogs."; var new_s = UpdateString("$",5); // new_s should equal "The q$uick $brown$ fox $jumps$ over$ the $lazy $dogs.$" 目标是使用此函数将&shy插入长字符串以允许它们进行换行。 也许有人知道更好的方法?


12
在ToString()之前检查null
这是场景... if (entry.Properties["something"].Value != null) attribs.something = entry.Properties["something"].Value.ToString(); 虽然有效且可以正常工作,但对我来说这看起来很丑。如果我在执行ToString()之前不检查null,那么如果该属性为null,它将引发异常。有没有更好的方法来处理这种情况? 非常感激!
76 c#  .net  string  properties 

9
Java赋值运算符执行
在Java中,我知道赋值的结果是正确的操作数的值,因此类似语句的结果x == (y = x)为to true。 但是,此代码输出false。 public static void main(String[]args){ String x = "hello"; String y = "goodbye"; System.out.println(x.equals(x = y)); } 为什么是这样?以我的理解,它首先求值(x = y),它分配x的值y,然后返回的值y。然后x.equals(y)进行了评估,应该是true从那时起x,y现在应该共享相同的引用,但是我得到了false。 这是怎么回事


2
在同一分隔符上多次拆分NSString
我目前收到这样的字符串: @"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54" 我将其拆分为: testArray = [[NSArray alloc] init]; NSString *testString = [[NSString alloc] initWithFormat:@"Sam|26,Hannah|22,Adam|30,Carlie|32,Jan|54,Steve|56,Matty|24,Bill|30,Rob|30,Jason|33,Mark|22,Stuart|54,Kevin|30"]; testArray = [testString componentsSeparatedByString:@","]; dict = [NSMutableDictionary dictionary]; for (NSString *s in testArray) { testArray2 = [s componentsSeparatedByString:@"|"]; [dict setObject:[testArray2 objectAtIndex:1] forKey:[testArray2 objectAtIndex:0]]; } 我现在将收到这样的字符串: @"Sam|26|Developer,Hannah|22|Team Leader,Adam|30|Director,Carlie|32|PA,Jan|54|Cleaner" 我可以(以及如何)使用与上述相同的方法使用“ |”多次分隔字符串。分隔器?


4
如何通过空格分隔字符串,并使用正则表达式忽略开头和结尾的空格成单词数组?
我通常在JavaScript中使用以下代码按空格分割字符串。 "The quick brown fox jumps over the lazy dog.".split(/\s+/); // ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."] 即使单词之间有多个空格字符,这当然也可以使用。 "The quick brown fox jumps over the lazy dog.".split(/\s+/); // ["The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog."] 问题是当我有一个带有前导或尾随空格的字符串时,在这种情况下,所得的字符串数组将在该数组的开头和/或结尾包含一个空字符。 " The quick brown fox jumps over the lazy …

3
为什么从Razor View接收POST请求时,我得到的是null而不是空字符串?
我以前没有值时会收到一个空字符串: [HttpPost] public ActionResult Add(string text) { // text is "" when there's no value provided by user } 但是现在我通过一个模型 [HttpPost] public ActionResult Add(SomeModel Model) { // model.Text is null when there's no value provided by user } 所以我必须使用?? ""运算符。 为什么会这样呢?

7
如何将整个流读入std :: string?
我正在尝试将整个流(多行)读入字符串。 我正在使用此代码,并且可以工作,但是却冒犯了我的风格感……当然,有一种更简单的方法吗?也许使用stringstreams? void Obj::loadFromStream(std::istream & stream) { std::string s; std::streampos p = stream.tellg(); // remember where we are stream.seekg(0, std::ios_base::end); // go to the end std::streamoff sz = stream.tellg() - p; // work out the size stream.seekg(p); // restore the position s.resize(sz); // resize the string stream.read(&s[0], sz); // and …
76 c++  string  stream 

13
每N个字符在字符串中添加分隔符?
我有一个包含二进制数字的字符串。如何在每8位数字后分隔字符串? 假设字符串是: string x = "111111110000000011111111000000001111111100000000"; 我想在每8个字符后添加一个分隔符,例如,(逗号)。 输出应为: "11111111,00000000,11111111,00000000,11111111,00000000," 然后,我想将其发送到list <>最后8个字符,然后再发送到前面的8个字符(,除外),依此类推。 我怎样才能做到这一点?

7
拥有String.Replace的方式只打“整个单词”
我需要一种方法来做到这一点: "test, and test but not testing. But yes to test".Replace("test", "text") 返回此: "text, and text but not testing. But yes to text" 基本上我想替换整个单词,而不是部分匹配。 注意:为此,我将不得不使用VB(SSRS 2008代码),但是C#是我的常规语言,因此使用两种方法都可以。
76 c#  .net  vb.net  string 

8
在Python中给定索引处的给定字符串中插入一些字符串
我是Python的新手,面临一个问题:如何在已经存在的字符串中插入一些字段? 例如,假设我从任何包含以下内容的文件中读取了一行: line = "Name Age Group Class Profession" 现在,我必须在Class字段之前的同一行中多插入3rd Field(Group)3次。这意味着输出线应为: output_line = "Name Age Group Group Group Group Class Profession" 我可以轻松地检索第三个字段(使用split方法),但是请让我知道插入字符串的最简单方法吗?
76 python  string 

7
c ++中c_str函数的用途是什么
我刚刚开始阅读C ++,发现c ++具有丰富的字符串操作功能,而C语言则没有。我正在阅读这些函数并且遇到了c_str(),据我所知是c_str将可能以null终止或可能不是null终止的字符串转换为字符串。 谁能建议我一些示例,以便我可以理解c_str函数的使用?
76 c++  c  string 


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.