Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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

57
votes
9answers
11k 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. ...
45
votes
3answers
4k 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 ...
45
votes
3answers
2k 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 ...
40
votes
9answers
6k 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: <...
36
votes
2answers
903 views

String-manipulation bugs in v10.1

Introduced in 10.1, fixed in 10.2; fixed via paclet update in 10.1. The new version 10.1 has introduced some strange (possibly buggy) behaviour compared to v10.0: ...
36
votes
1answer
916 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: ...
34
votes
4answers
4k 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 ...
31
votes
6answers
1k 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 ...
31
votes
2answers
3k views

How do I perform string matching and replacements?

What are, and how do I use Mathematica's string matching and replacement tools?
29
votes
8answers
2k 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 ...
27
votes
1answer
819 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 ...
26
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 ...
25
votes
4answers
2k 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 ...
25
votes
1answer
330 views

StringCases is Confused with Patterns in Version 10.0.1

Bug introduced in 10.0.1 and fixed in 10.0.2 I observed the following with StringCases when trying to use rules/patterns. Here is a screenshot of the behavior: ...
23
votes
5answers
5k 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 ...
23
votes
7answers
7k 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 ...
22
votes
3answers
951 views

Typoglycemia text manipulation

I was just reading the Wikipedia page on Typoglycemia: ... readers can understand the meaning of words in a sentence even when the interior letters of each word are scrambled ... and wanted to ...
22
votes
5answers
466 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: ...
21
votes
3answers
1k views

Solving word search puzzles

I am trying to create a code that can identify the following terms in a grid of letters: MATHEMATICA, STACK, EXCHANGE and USERS. ...
21
votes
1answer
537 views

Why Except is so slow as compared to equivalent RegularExpression?

When comparing performance of the solutions suggested for this question I discovered that StringExpression involving Except is ...
21
votes
1answer
1k 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 ...
21
votes
2answers
637 views

Why is TextString[-0.5] a positive number?

Bug introduced in 10.0 and 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 ...
21
votes
1answer
276 views

Can Mathematica convert a RegularExpression into a StringExpression or vice-versa?

I know that Mathematica can perform string comparison using either StringExpression or RegularExpression. Although regular ...
19
votes
7answers
2k 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. ...
19
votes
4answers
3k 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: <...
19
votes
3answers
870 views

Unicode-aware string functions?

I've recently discovered that ToUpperCase is quite unreliable on non-ASCII input: ...
19
votes
4answers
2k 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 board ...
18
votes
1answer
759 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
10answers
2k views

Match patterns 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". Result:...
17
votes
6answers
975 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
7answers
564 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 ...
17
votes
5answers
808 views

Opposite of StringSplit

What is an appropriate command that does the opposite of the following? StringSplit["a b c d e f g"," "]
17
votes
2answers
272 views

Unexpected behaviour when pattern matching with Longest

I came across some unexpected behaviour today when using the Longest function when trying to do some pattern matching. ...
17
votes
1answer
326 views

String operation 1000X slower for special characters

It seems that the string operation is very slow if we have special characters in the string. Consider this example, ...
17
votes
3answers
681 views

Highlighting pattern strings in a large text

I would like to apply Style to strings that are matched, for example, with one or more specified patterns or regular expressions, a string or a list of both, ...
17
votes
2answers
222 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 ...
17
votes
2answers
1k 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 ...
17
votes
2answers
277 views

Mma 10.2 `Alphabet` and `LanguageData` give different and both erroneous list of letters

Bug introduced in 10.2 and fixed in 10.3 The new function Alphabet introduced in Mathematica 10.2 gives the wrong list of letters for the Spanish language in my ...
16
votes
6answers
719 views

Type conversions

I have the following list, each element of which is of type string: {"abc", "def","2","ghi","7"} Is there an efficient way to arrive at: ...
16
votes
9answers
12k 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 ...
16
votes
3answers
1k views

How do I make a string literal without having to escape backslashes?

I'd like to say something like: foo = @"\abc\def\ghi" instead of foo = "\\abc\\def\\ghi" How do I do this? This would ...
16
votes
2answers
3k views

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

Both of the following return True: ...
15
votes
3answers
504 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 ...
15
votes
3answers
1k 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 ...
15
votes
4answers
480 views

balanced Shortest[] and string-patterns

The pattern Shortest["A"~~__~~"B"] is oriented : It assumes the text is read from left to right and it takes the text between the first "A" and the next "B". Any "A"...
15
votes
1answer
394 views

StringReplace, ReplaceAll and Rule interact in a bizarre way

I have the following pair of things: ClearAll[foo, labeledFoo]; labeledFoo = {"FooBarBazQuux", foo}; This works like you'd expect: ...
14
votes
4answers
2k views

What stopwords list is the Wolfram language using?

The documentation of DeleteStopwords only says that it "uses a standard, built-in list of stopwords". So what is it exactly? Update Now that it is said "standard"...
14
votes
2answers
475 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 ...
14
votes
6answers
373 views

How can I mix the behaviors of StringReplace and ReplaceAll in a single rule?

I'm trying to take a list of the form { {"65 + 3 months", 75}, {"65 + 4 months", 75.1} }, and transform the string part to a number such as 65.25 or 65.333, ...
14
votes
2answers
455 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 ...