I've noticed a lot of programming introductions (almost any language) usually include a heavy barrage of string manipulation quite early, such as:

  • Count the number of "xx" in the given string. We'll say that overlapping is allowed, so "xxx" contains 2 "xx".
  • Reverse the given string and remove all vowels from it.

Not just online courses, but even in degree programmes.

But the thing is, I've never encountered a situation in "real life" where you had to do anything like that. Is it just luck, or do you really need to do heavy manipulation in the above manner in some fields of programming? Can you give a description or an example where it is needed?

Just to clarify, not necessarily string processing itself as in echoing variables to a html template, or other basic stuff like that, but the more arcane type of processing, like reading every second character? But yes, perhaps it is just for training purposes instead of direct needs. The file format conversion is a good note, though.

  • I've used a lot of string manipulation in projects where I've read from an old database into a newly designed database (having to strip out several parts of a specific field before adding each separate part to a different field in a new record in the new database), and when dealing with streams of text read from COM and HTTP ports. Not a typical set of scenarios, but those are some of the tasks I've used them in – Jamie Taylor Sep 24 '13 at 15:09
  • 2
    The kind of example you show seem mostly because they are easy first steps in using loops. Strings have the slight advantage for beginners that they are 'real world' objects, things that are easier to understand than more abstract things like arrays of numbers. But there are a lot of places where you actually do a lot of string manipulation, web development is one of them. – thorsten müller Sep 24 '13 at 15:10
  • 1
    String manipulation is one of the most common things in computer programming. – Tulains Córdova Sep 24 '13 at 16:18
  • 3
    +1 Because I don't believe the question deserves all the downvotes. It's a perfectly reasonable question. – MarkJ Sep 24 '13 at 16:19
  • Thing about that sort of question is that it's a good way to test useful knowledge like lists/arrays and so on. You could do what I did, and have your students (I was a TA) write real-world programs in Prolog (a complete travel planner using graph traversal) but you need to already know basic Prolog to do that. Meanwhile, counting the amounts of "xx" sublists in a list in Prolog is something like count([_], 0). count([x|[x|Tail]], Count). count([X|[Y|Tail]], Count). and then filling in some implications. It's really easy. – Haakon Løtveit Aug 16 '17 at 11:34

Real life programming will almost certainly involve string manipulation at one point or another. Your example is a very specific case and may or may not appear in a real life scenario (In reality, I'm sure somebody, somewhere has had to tackle that problem in real life).

However, the real life applicability of a particular use case is not necessarily the point of those exercises when introducing a new language or teaching someone brand new to programming. The point of those exercises are likely to be any of the following (depending on context):

  1. To teach a programmer the basic syntax of a given language using an easily understood problem.
  2. To provide an exercise allowing novice programmers to learn basic programming logic without having to understand a complex problem.
  3. To express a languages ability to solve a particular string manipulation problem.
| improve this answer | |
  • I definitely agree. How many times have you ever had to say the phrase, "See Jane get the ball. Go, Jane, go!" in your lifetime? However, that isn't to say it is worthless since the point is understanding, not application (at least not at first). – Neil Sep 24 '13 at 15:49

Text files and text data are everywhere.

Converting .csv to .xml, reading .py for execution or .f90 for compilation, building and sending spam from a list of mail addresses, building a database of keywords for indexing the web: all of that is string manipulation.

Do computers do something else than manipulating strings?

| improve this answer | |
  • 1
    I would be inclined say: Compute ;) – Juha Untinen Sep 24 '13 at 15:18
  • @JuhaUntinen: From some points of view, you could say that computing is string manipulation. A Turing machine just goes back and forth over a string of 1's and 0's and is constantly manipulating that string... – FrustratedWithFormsDesigner Sep 24 '13 at 15:35
  • Well, they manipulate integer values too, and more often than they perform string manipulation - mainly as each string manipulation usually requires a much larger amount of integer manipulation. – gbjbaanb Sep 25 '13 at 11:16

String manipulation actually is fairly common when you're doing I/O, which includes most network programming. Unicode complicates things, of course, but text remains the closest thing we have to a universal interface.

But where it really shines is as a metaphor. Working with individual characters in strings is, in many ways, not unlike working with cells in memory. At the lowest levels, the operations can be almost identical. This makes strings a less abstract way of learning the very basics of working with data at low levels, and that makes it valuable to learn early.

| improve this answer | |

These are very specific examples, really just given as training for string manipulation.

And it is more common than you think - mostly in parsing and UI code.

For example - parsing dates out of strings - this requires extensive string manipulation.

Or parsing HTML into a DOM - same kind of thing.

You will also need to manipulate strings for UI purposes - constructing strings based on specific data and conditions.

| improve this answer | |

Is it just luck, or do you really need to do heavy manipulation in the above manner in some fields of programming?

There are domains where string manipulation is not a necessity, or it is a marginal problem. There are also domains where it is a critical requirement.

For example, web technologies depend critically on text manipulation (web and mail servers and clients); so do most software development tools (IDEs, editors, compilers, etc).

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.