Questions on the manipulation of String objects in Mathematica, and the functions used for these manipulations.

learn more… | top users | synonyms (2)

38
votes
3answers
1k views

Visualizing the difference between two sequences (strings)

I'm trying to develop a way of comparing two sequences (probably originally text, such as text writing or source code, possibly converted to lists). As a familiar example, consider the revisions ...
36
votes
8answers
5k views

How do you convert a string containing a number in C scientific notation to a Mathematica number?

Suppose I have a string containing the C-representation of a floating point number, for example s = "1.23e-5" and I want to convert this to a Mathematica number. ...
31
votes
1answer
582 views

Why is StringExpression faster than RegularExpression?

Edit: as noted by Albert Retey the performance difference is only seen when sub expression extraction is performed. If this test is used below the timings are similar: ...
30
votes
9answers
5k views

Programming paradigm change

I'm new to Mathematica, I've been learning by myself, and, being a Java developer for some time, it makes it hard to adapt. I've tried to solve a Reddit challenge, and I came up with this solution: ...
30
votes
2answers
662 views

String-manipulation bugs in v10.1

Fixed as of 2015-04-06 via paclet update. The new version 10.1 has introduced some strange (possibly buggy) behaviour compared to v10.0: ...
29
votes
4answers
3k views

Looking for “Longest Common Substring” solution

I'm looking for robust code to solve the "Longest Common Substring" problem: Find the longest string (or strings) that is a substring (or are substrings) of two or more strings. I can just code it ...
27
votes
3answers
2k views

How to join two Style[]d strings

Does anyone know whether it is possible to combine\join two styled strings? That is, while the following code works fine: omega = "text"; omega<>omega ...
26
votes
1answer
665 views

Convenient string manipulation

With Mathematica I always feel that strings are "second class citizens." Compared to a language such as PERL one must juggle a lot of code to accomplish the same task. The available functionality is ...
25
votes
7answers
1k views

Splitting words into specific fragments

I am looking into splitting words into a succession of chemical elements symbols, where possible. For example: Titanic = Ti Ta Ni C (titanium, tantalum, nickel, carbon) A word may or may not be ...
24
votes
6answers
730 views

Partition string into chunks

This seems like it should be trivial, but how do I partition a string into length n substrings? I can of course write something like ...
23
votes
4answers
1k views

What is the fastest way to count square-free words?

Background A word is a string of letters in an alphabet. A square-free word has no adjacent repeating substring. For example, (in the ternary alphabet of {0,1,2}) the words 00, 012121, and 0212012021 ...
23
votes
1answer
282 views

StringCases is Confused with Patterns in Version 10.0.1

I observed the following with StringCases when trying to use rules/patterns. Here is a screenshot of the behavior: Not only is the pattern highlighted in red, ...
20
votes
7answers
1k views

Elegant and fast way to join all string sequences

I want to do what title says: {"a","b",1,2,3,4,"a",2,2,2,"edg","?"} => {"ab",1,2,3,4,"a",2,2,2,"edg?"} in the most elegant way. For example one replacement rule for ...
20
votes
2answers
1k views

How do I perform string matching and replacements?

What are, and how do I use Mathematica's string matching and replacement tools?
20
votes
2answers
3k views

How to import all files of a folder at once?

I know how to import one textfile by calling its name filestring = Import["myfile.tex", "Text"]; Then "filestring" is a string with the myfile content. How do ...
19
votes
1answer
409 views

How to strip all formatting from a string and convert it to plain text?

The documentation on String states: But if I try to perform some manipulations on this string, for example, take 2 words from the left, I get some shards of ...
18
votes
3answers
354 views

Unexpected result {“.a”, “co”, “.m”} from Sort[{“.m”, “.a”, “co”}]

I came across the following situation: Evaluating Sort[{".m", ".a", "co"}] Results in {".a", "co", ".m"} I wondering: ...
18
votes
1answer
521 views

NotebookFind and String Pattern Expressions

Is there a way that NotebookFind can be used to match string pattern expressions rather than just strings? The documentation for ...
17
votes
6answers
760 views

Split a string at specific positions

Given a string of alphanumerical characters, how to split it simply and quickly at the center of continuous letter-substrings? Is there an elegant and fast solutions out there in the "computational ...
17
votes
8answers
3k views

How to express an integer number in English words?

How do I convert a number to a readable string? I would like to implement a function, inWords[], inWords[n_]:= ? which ...
17
votes
3answers
521 views

Unicode-aware string functions?

I've recently discovered that ToUpperCase is quite unreliable on non-ASCII input: ...
16
votes
4answers
963 views

Finding all dictionary words that can be made with a given set of characters (Wordfeud/Scrabble)

This is a slightly more involved variant of this recent question. By sheer coincidence I happened to have been building a Wordfeud (Scrabble clone) game in Mathematica. My interactive Wordfeud ...
16
votes
2answers
556 views

Why is TextString[-0.5] a positive number?

Fixed in 10.0.2 I want to format numbers in decimal notation and chose TextString for this task. But today, when looking at the final result of my 3-weeks ...
16
votes
2answers
758 views

Lexicographic ordering of strings in Mathematica

I recently realized that Mathematica seems incapable of comparing strings in the "normal" expected lexicographic order. Indeed, for some simulations, I need to process text directly, without fiddling ...
15
votes
6answers
475 views

Splitting a list by specifying section headers

I have a list of strings called mylist: mylist = {"[a]", "a", "a", "[b]", "b", "b", "[ c ]", "c", "c"}; I would like to split ...
15
votes
3answers
471 views

Grabbing a number of characters around a string pattern

If I have some string data: text = StringTake[ExampleData[{"Text", "DeclarationOfIndependence"}], 400] "When in the Course of human events, it becomes ...
14
votes
5answers
2k views

sprintf() or close equivalent, or re-implementation?

Is there a sprintf() command (some command that takes a printf-style format string and a list of values to insert into the ...
14
votes
8answers
900 views

DeleteCases on list of Strings

Consider the following: data={"AB","CD","AF"}; Now I would like to delete all String from data which starts with "A". ...
14
votes
4answers
1k views

Converting a large floating-point number to a single-line string

When I feed a large (or small) enough floating-point number to ToString, it produces a two-line string, with the first line containing only the exponent of 10: ...
14
votes
2answers
195 views

Bug in Operator Form of StringCases

fixed in 10.1 (windows) According to the documentation there's an operator form for StringCases ?StringCases Here is the ...
14
votes
2answers
325 views

Haiku Extractor

Inspired by the cool Tumblr Haiku Times that searches for accidental haiku in New York Times articles, I tried my hand at implementing such a search in Mathematica for my own text samples. I'm still ...
13
votes
7answers
763 views

How to sort digits in a string while keeping other characters untouched?

Now there is a string with digits and letters, for instance, "95uge678r3gi89hgfe30kgh063d51". And the expected output is ...
13
votes
3answers
672 views

How to remove accents from text?

I would like to know how I can remove accents from a string. For example, how can I transform "string test áéíóú" into ...
13
votes
3answers
324 views

Transform fancy usage messages in 1D string

When we look at the usage messages of built-in functions nowadays (not in the good old times, when they were a simple descriptions) we see that although they look pretty in the front end, it is really ...
13
votes
2answers
506 views

How to embed an image into a string?

Note: The bug described in the post is in Mathematica 9, and has been fixed in 10.0. The documentation for String contains the following statements: Strings ...
12
votes
3answers
683 views

How to capitalize the first letter of each word in a string

The following examples only work for the first character of the whole string, but not of words. ...
12
votes
4answers
509 views

How do you get a list of filenames from a URL?

How would I get a list of filenames available at a URL? For example, list the files at: http://www1.ncdc.noaa.gov/pub/data/cmb/drought/weekly-palmers/2005/
12
votes
5answers
591 views

How to remove duplicate characters from a string?

How do you remove duplicate characters in a string without converting the string to a list. I want to delete all duplicate characters in a string (i.e. get the unsorted union of characters). For ...
12
votes
2answers
364 views

Split a Unicode string maintaining uppercase characters

I want to split a string according to a predefined set of substrings (lowercase), though the actual text can contain uppercase characters anyplace. The task is to find the matches, longer preferred ...
12
votes
1answer
634 views

Rearranging a Polynomial

In Mathematica 8.04 on Windows, I want to display a formula in standard textbook format. The formula is the variance of an $N$-security portfolio. For two securities it is: ...
11
votes
9answers
4k views

How do I extract a number from a string?

I want to extract the number from an alphanumeric string. This is what I tried: StringTake["thiru3", {6, 6}] The result I got is ...
11
votes
7answers
656 views

Splitting strings into letter keys and integers

I have strings of the following form: string = "ABC123DEFG456HI89UZXX1"; Letter keys of variable lengths are followed by (positive) integers. I want to get this ...
11
votes
7answers
226 views

DateList coupled with String Operations

Through some webscraping, I have the following list: ...
11
votes
3answers
703 views

Specifying string patterns in DeleteCases

It seems that DeleteCases is not compatible with string patterns, at least directly (see, for example, this question). Is this true? If so, why is this the case? ...
11
votes
5answers
2k views

What is the fastest way to extract a substring from a huge string?

I develop an algorithm to find the most common substrings of a given length n up to a certain distance l in front of a pattern p in a huge sequence S. E.g. I want to find the most common hexamers ...
11
votes
3answers
291 views

ToExpression fails with long nested string expressions

Consider the following conversion from String to Mathematica Expression: ...
11
votes
2answers
240 views

Pasting “rectangles” of text side-by-side

In Emacs/Xemacs, there is a rectangle feature, such that you can delete, copy, etc., "rectangles" of text. Now suppose I have two text files, each containing one column of numbers. Is it possible to ...
11
votes
2answers
399 views

Should I use \\\“ or \” to match " in regex?

Both of the following return True: ...
11
votes
1answer
392 views

How to convert a notebook cell to a string retaining all formatting, colorization of identifiers etc?

I have an opened Mathematica notebook containing several cells. Suppose, I am interested in one of them -- it may contain a complete or incomplete expression (e.g. with syntax errors, highlighted ...
10
votes
4answers
482 views

How does string interpolation work in Mathematica

I've noticed that Mathematica 8 seems to have some kind of string interpolation feature, but I couldn't find any documentation on it and I can't figure out how it works. For example, if I enter ...