Hot answers tagged

93

Readability. The format string syntax is more readable, as it separates style from the data. Also, in Python, %s syntax will automatically coerce any non str types to str; while concatenation only works with str, and you can't concatenate str with int. Performance. In Python str is immutable, so the left and right string have to be copied into the new string ...


59

It's anyways bad practice to initialie a char array with a string literal. The author of that comment never really justifies it, and I find the statement puzzling. In C (and you've tagged this as C), that's pretty much the only way to initialize an array of char with a string value (initialization is different from assignment). You can write either ...


56

is it appropriate to hard-code strings that are very unlikely to change during the lifetime of an application? Of course I can't guarantee 100% that they won't change, but the risk vs cost is almost trivial to weigh in my eyes - hardcoding is the better idea here It looks to me that you answered your own question. One of the biggest challenges we face is ...


50

Am I the only one who reads left to right? To me, using %s is like listening to German speakers, where I have to wait until the end of a very long sentence to hear what the verb is. Which of these is clearer at a quick glance? "your %s is in the %s" % (object, location) or "your " + object + " is in the " + location


47

Because it used to be UCS-2, which was a nice fixed-length 16-bits. Of course, 16bit turned out not to be enough. They retrofitted UTF-16 in on top.


45

Yes, storing strings instead of numbers can use more space. The reason that high-profile pltforms are doing it anyway is that they think the benefits of that solution are greater than the cost. What are the benefits? You can easily read a database dump and understand what it's about without memorizing the enum tables, and even semi-official GUIs might ...


42

The things that are called "C strings" will be null-terminated on any platform. That's how the standard C library functions determine the end of a string. Within the C language, there's nothing stopping you from having an array of characters that doesn't end in a null. However you will have to use some other method to avoid running off the end of a string....


40

Historically (perhaps by rewriting parts of it), it was the contrary. On the very first computers of the early 1970s (perhaps PDP-11) running a prototypical embryonic C (perhaps BCPL) there was no MMU and no memory protection (which existed on most older IBM/360 mainframes). So every byte of memory (including those handling literal strings or machine code) ...


39

I was mistaken, there is a set of "Xtos" functions, they are all just named to_string. Each to_string is overloaded to take a different basic type, i.e.: std::string to_string(float f); std::string to_string(int f); ... See here for more info.


38

It depends on the kind of search you want to perform. Each of the algorithms performs particularly well for certain types of a search, but you have not stated the context of your searches. Here are some typical thoughts on search types: Boyer-Moore: works by pre-analyzing the pattern and comparing from right-to-left. If a mismatch occurs, the initial ...


28

If the language you are using supports the use of enums, I'd use them. It allows you to limit the number of options available for a given type. e.g. in Java: public enum Direction { LEFT, RIGHT, UP, DOWN }


27

String concatenation can be slow. If you just want to go "old school", and speed is important, then forget storing the current word, and continually concatenating letters onto it. Just store the start of the current word, as an index into the array, and the start of the longest word. Store the length of the current word, and the length of the longest. ...


25

You are reasoning at the wrong scope. You haven't hardcoded only individual verbs. You have hardcoded the language and its rules. This, in turn, means that your application cannot be used for any other language, and cannot be extended with other rules. If this is your intent (i.e. using it for French only), this is the right approach, because of YAGNI. But ...


23

In the c++11 standard (I am reading N 3290 version), chapter 21.4.7.1 speaks about the c_str() method : const charT* c_str() const noexcept; const charT* data() const noexcept; Returns: A pointer p such that p + i == &operator for each i in [0,size()]. Complexity: constant time. Requires: The program shall not alter any of the values stored in ...


23

You definitely need NLP. For example: EX P ER T S E XC H A NG E may either mean EXPERT SEX CHANGE or EXPERTS EXCHANGE depending on context.


22

If you consider translation to be important in your project, the first syntax will really help with it. For instance you may have: static final string output_en = "{0} is {1} years old."; static final string output_fr = "{0} a {1} ans."; int age = 10; string name = "Henri"; System.out.println(string.Format(output_en, name, age)); System.out.println(...


22

A PHP string is just a sequence of bytes, with no encoding tagged to it whatsoever. String values can come from various sources: the client (over HTTP), a database, a file, or from string literals in your source code. PHP reads all these as byte sequences, and it never extracts any encoding information. As long as all your data sources and destinations use ...


22

The only difference between the two is the synchronization used in StringBuffer. The overhead of synchronization is not huge in the grand scheme of things, but it is significant relative to the StringBuilder methods that don't have them. The JVM is doing work that it wouldn't otherwise have to do--especially with only one thread, etc. If your code works ...


22

You are confusing two different things: Immutable means the object's memory contents cannot be modified. When you modify an immutable object (e.g, a string), the memory contents of this object are not modified. Instead: A new block of memory is allocated. The contents of the object you (tried to) modify is copied to this new block, with the part you wanted ...


21

Your explanation why it is inefficient is accurate, in at least the languages I am familiar with (C, Java, C#), though I would disagree that it is universally common to perform massive amounts of string concatenation. In the C# code I work on, there is copious usage of StringBuilder, String.Format, etc. which are all memory saving techiniques to avoid over-...


21

Reasons I can recall : String Pool facility without making string immutable is not possible at all because in case of string pool one string object/literal e.g. "XYZ" will be referenced by many reference variables , so if any one of them changes the value others will be automatically gets affected . String has been widely used as parameter for many java ...


21

Determination of the terminating character is up to the compiler for literals and the implementation of the standard library for strings in general. It isn't determined by the operating system. The convention of NUL termination goes back to pre-standard C, and in 30+ years, I can't say I've run into an environment that does anything else. This behavior ...


20

You're confusing immutability of reference to object and immutability of the object itself. They are separate things. laptop.memory is just a reference (pointer) to the object. This can be modified, i.e. you can change the reference to point to a different object - which is exactly what you did - you created a second String object containing "2GB" and ...


18

This is called Natural Language Processing and it's a huge, complex field. Something like you describe is a monumental achievement, and even the best solutions, like Watson, are nowhere near perfect. Things like this make it challenging: "Buffalo buffalo Buffalo buffalo buffalo buffalo Buffalo buffalo" a grammatically correct sentence in American ...


18

For me, a string literal is a language construct, an hardcoded string is a string whose value is fixed in a program (you may discuss if it needs to be fixed at compile time or could be somewhat determined by some runtime consideration). An hardcoded string is often a string literal but could come from other sources (say concatenation of character literals ...


18

Does the functionality taking the value need to take ownership of the string? If so use std::string (non-const, non-ref). This option gives you the choice to explicitly move in a value as well if you know that it won't ever be used again in the calling context. Does the functionality just read the string? If so use std::string_view (const, non-ref) this is ...


17

Is the ID always in the form: IO123456? What your colleague could mean is that he only sends the numeric part, which fits easily in 4 bytes omitting the "IO" part.


Only top voted, non community-wiki answers of a minimum length are eligible