string-concatenation

String concatenation vs. interpolation in Ruby

倾然丶 夕夏残阳落幕 提交于 2019-11-26 22:07:14
I am just starting to learn Ruby (first time programming), and have a basic syntactical question with regards to variables, and various ways of writing code. Chris Pine's "Learn to Program" taught me to write a basic program like this... num_cars_again= 2 puts 'I own ' + num_cars_again.to_s + ' cars.' This is fine, but then I stumbled across the tutorial on ruby.learncodethehardway.com, and was taught to write the same exact program like this... num_cars= 2 puts "I own #{num_cars} cars." They both output the same thing, but obviously option 2 is a much shorter way to do it. Is there any

How do I concatenate strings in Swift?

 ̄綄美尐妖づ 提交于 2019-11-26 21:32:06
How to concatenate string in Swift? In Objective-C we do like NSString *string = @"Swift"; NSString *resultStr = [string stringByAppendingString:@" is a new Programming Language"]; or NSString *resultStr=[NSString stringWithFormat:@"%@ is a new Programming Language",string]; But I want to do this in Swift-language. Fogmeister You can concatenate strings a number of ways: let a = "Hello" let b = "World" let first = a + ", " + b let second = "\(a), \(b)" You could also do: var c = "Hello" c += ", World" I'm sure there are more ways too. Bit of description let creates a constant. (sort of like an

How do I concatenate strings and variables in PowerShell?

穿精又带淫゛_ 提交于 2019-11-26 21:11:07
Suppose I have the following snippet: $assoc = New-Object psobject -Property @{ Id = 42 Name = "Slim Shady" Owner = "Eminem" } Write-host $assoc.Id + " - " + $assoc.Name + " - " + $assoc.Owner I'd expect this snippet to show: 42 - Slim Shady - Eminem But instead it shows: 42 + - + Slim Shady + - + Eminem Which makes me think the + operator isn't appropriate for concatenating strings and variables. How should you approach this with PowerShell? Write-Host "$($assoc.Id) - $($assoc.Name) - $($assoc.Owner)" See the Windows PowerShell Language Specification Version 3.0 , p34, sub-expressions

Using LINQ to concatenate strings

走远了吗. 提交于 2019-11-26 21:02:36
What is the most efficient way to write the old-school: StringBuilder sb = new StringBuilder(); if (strings.Count > 0) { foreach (string s in strings) { sb.Append(s + ", "); } sb.Remove(sb.Length - 2, 2); } return sb.ToString(); ...in LINQ? Jorge Ferreira This answer shows usage of LINQ ( Aggregate ) as requested in the question and is not intended for everyday use. Because this does not use a StringBuilder it will have horrible performance for very long sequences. For regular code use String.Join as shown in the other answer Use aggregate queries like this: string[] words = { "one", "two",

Concatenation operator (+) vs. concat() [duplicate]

余生长醉 提交于 2019-11-26 20:54:01
问题 This question already has answers here : String concatenation: concat() vs “+” operator (11 answers) Closed 5 years ago . For string concatenation we can use either the concat() or concat operator (+) . I have tried the following performance test and found concat() is faster and a memory efficient way for string concatenation. String concatenation comparison for 100,000 times : String str = null; //------------Using Concatenation operator------------- long time1 = System.currentTimeMillis();

Efficiently repeat a character/string n times in Scala

霸气de小男生 提交于 2019-11-26 20:05:57
问题 I would like to do the following more efficiently: def repeatChar(char:Char, n: Int) = List.fill(n)(char).mkString def repeatString(char:String, n: Int) = List.fill(n)(char).mkString repeatChar('a',3) // res0: String = aaa repeatString("abc",3) // res0: String = abcabcabc 回答1: For strings you can just write "abc" * 3 , which works via StringOps and uses a StringBuffer behind the scenes. For characters I think your solution is pretty reasonable, although char.toString * n is arguably clearer.

Does concatenating strings in Java always lead to new strings being created in memory?

柔情痞子 提交于 2019-11-26 19:01:06
I have a long string that doesn't fit the width of the screen. For eg. String longString = "This string is very long. It does not fit the width of the screen. So you have to scroll horizontally to read the whole string. This is very inconvenient indeed."; To make it easier to read, I thought of writing it this way - String longString = "This string is very long." + "It does not fit the width of the screen." + "So you have to scroll horizontally" + "to read the whole string." + "This is very inconvenient indeed."; However, I realized that the second way uses string concatenation and will create

How to concatenate Strings in EL expression?

孤街醉人 提交于 2019-11-26 18:56:17
I need to create a callback for <h:commandButton> while as a parameter I need to pass an argument that is string-concatenated with an external parameter id: I tried nesting an EL expression something like this: <h:commandButton ... action="#{someController.doSomething('#{id}SomeTableId')}" /> However this failed with an EL exception. What is a right syntax/approach to do this? BalusC If you're already on EL 3.0 (Java EE 7; WildFly, Tomcat 8, GlassFish 4, etc), then you could use the new += operator for this: <h:commandButton ... action="#{someController.doSomething(id += 'SomeTableId')}" /> If

Is this time complexity actually O(n^2)?

余生颓废 提交于 2019-11-26 18:48:36
I am working on a problem out of CTCI. The third problem of chapter 1 has you take a string such as 'Mr John Smith ' and asks you to replace the intermediary spaces with %20 : 'Mr%20John%20Smith' The author offers this solution in Python, calling it O(n): def urlify(string, length): '''function replaces single spaces with %20 and removes trailing spaces''' counter = 0 output = '' for char in string: counter += 1 if counter > length: return output elif char == ' ': output = output + '%20' elif char != ' ': output = output + char return output My question: I understand that this is O(n) in terms

String equals and == with String concatenation [duplicate]

孤人 提交于 2019-11-26 18:35:13
问题 This question already has an answer here: Getting strange output when printing result of a string comparison 3 answers I am trying to understand the String concatenation with the output of String compare. To be clear, i have the class to compare two strings using == and equals. I am trying to concat the output of == and equals() to a string. The output of equals() concats, but the output of == does not concat. Using boxing feature of java, the boolean concatenated with string will contact.