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

learn more… | top users | synonyms (2)

28
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 ...
35
votes
8answers
4k 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. ...
18
votes
2answers
991 views

How do I perform string matching and replacements?

What are, and how do I use Mathematica's string matching and replacement tools?
19
votes
2answers
2k 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 ...
23
votes
3answers
1k 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 ...
25
votes
1answer
617 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 ...
31
votes
1answer
509 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: ...
3
votes
5answers
368 views

How to write a function to remove comments from a .m source file preserving formatting such as line wrapping reasonably?

How to write a function to remove comments from a .m source file preserving formatting such as line wrapping reasonably? This means if I have ...
6
votes
2answers
241 views

Formatting text through pattern matching

I am trying to format some text based on some patterns. ...
6
votes
2answers
172 views

How to Clear variables represented as a list of strings?

Say I have a string list called fullpara fullpara={"width", "long", "line", "distance"} And there are corresponding variables ...
29
votes
9answers
4k 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: ...
10
votes
1answer
229 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: ...
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 ...
22
votes
6answers
635 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 ...
1
vote
3answers
311 views

Replace with position and order number

question conversion1 : Convert "[AA]AB[AA]BB[AA]B[AA]CCC" to "[A1A2]A3B1[A4A5]B2B3[A6A7]B4[A8A9]CCC" conversion2 : Convert ...
8
votes
2answers
2k views

Convert a string to hex number?

How can I convert a string containing a hex number such as "6b" to a hex digit that mathematica can use to do math with? I have tried using ...
2
votes
2answers
2k views

Faster way to convert real strings into numbers [duplicate]

Working with big files, I have to convert some numbers that are in string format into real MMA numbers. I know that I can use ToExpression but it's slow when ...
36
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 ...
7
votes
3answers
163 views

Converting StringJoin to StringForm inside Hold

Let's say I have a piece of code: Hold[{code1, "asdad " <> ToString[testa] <> " adsd " <> ToString[testb], code2}] (*MWE ofc*) ...
5
votes
3answers
317 views

Highlighting text with StringReplacePart but also using Style, Subscript

For a Natural Language Processing analysis task it's desirable to highlight a text string programmatically according to XML tags that reference specific words or phrases. The XML tags specify the ...
12
votes
2answers
344 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 ...
6
votes
4answers
283 views

Is there a more Mathematica-y way to label these plots?

I am studying an infinite square well in the context of quantum mechanics. ...
17
votes
3answers
479 views

Unicode-aware string functions?

I've recently discovered that ToUpperCase is quite unreliable on non-ASCII input: ...
17
votes
6answers
719 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 ...
14
votes
6answers
462 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 ...
9
votes
3answers
239 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 ...
6
votes
2answers
472 views

How to overload System`StringJoin to automatically use ToString on arguments?

I am using an overloaded version of the StringJoin function since years now, without any problem, as I've invested a lot of time and effort earlier to make its ...
18
votes
3answers
344 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: ...
10
votes
3answers
464 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 ...
7
votes
2answers
288 views

Revert FullForm-ed text to prettyprinted

Sometimes strings in a notebook/package file end up garbled due to some unknown/unintentional conversion of the expression/cell/notebook. I could not find a way to revert such unreadable text to its ...
6
votes
1answer
204 views

Dataset and string conversion, using Internal`StringToDouble

i'm trying to get familiar with the new Dataset-stuff in Mathematica 10. As it will (hopefully) provide some benefit for me, I have started to convert my app to move from list and list of rules to ...
5
votes
2answers
1k views

How to convert a string of number into real numbers without precision loss

I searched and did not find similar answers which can resolve the 'no precision loss' issue. A sample string is: ...
3
votes
3answers
510 views

Converting a string containing whitespace-delimited numbers to a list of numbers

Suppose I have a string str that contains only whitespace-delimited real numbers: ...
13
votes
2answers
454 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 ...
5
votes
3answers
2k views

Parameters in plot titles

My issue is how to mix strings with variables, specifically in the context of PlotLabel: I can put a string as my plot title, and I can also put a variable as a ...
2
votes
2answers
146 views

Difference between double quotes

I have a notebook and I enter something like PlotLabel->"my label with dynamic number "<>ToString[strange] and I get errors like However, the double ...
1
vote
0answers
100 views

Split one string about a fixed width with `SyntaxQ=True`

How to split one string about a fixed width, the string may come from the input or output cell of Notebook. sample string ...
16
votes
1answer
477 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 ...
13
votes
7answers
730 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". ...
15
votes
2answers
631 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 ...
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 ...
7
votes
5answers
354 views

Splitting strings at lower case characters and numbers

I'm trying to split the strings of chemicals into their elements and numbers. See this example "Fe3O4" will be split into ...
5
votes
3answers
336 views

Find the position of a string element, knowing only part of the string

I have a list, e.g.: list = {1, 2, 3, "Element 1", 4, 5, "Element 2", "Something else 1", "etcetera"} Now, I want all elements starting with ...
15
votes
3answers
460 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 ...
9
votes
1answer
125 views

Named string patterns in Alternatives, why does it depend on order?

When using the same name for part of patterns inside Alternatives how come it behaves differently for strings than in normal patterns? ...
9
votes
2answers
350 views

Keeping Quotation Marks in Output

I am running into the following seemingly simple problem. I have this code: testphrase="i love you"; input=ToLowerCase[StringSplit[testphrase, Whitespace]] It ...
8
votes
3answers
562 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? ...
7
votes
3answers
293 views

Unencoding encoded URLs in a function

Is there a built-in function for decoding encoded URLs in Mathematica? I would like to convert an encoded URL, for example ...
7
votes
3answers
225 views

How to turn a set of strings which represent a list into a 2D list

I have a set of data read in from a larger CSV file. I want it to match the format I have from another analysis. It is structured as a series of strings with letters and numbers Here it is copy-pasted ...
6
votes
1answer
259 views

String pattern search and replacement — it works, but produces error messages only the first time it is called

I defined a function in Mathematica to process a pre-generated string. More specifically, I'd like to process some parts of C codes generated from MMA. Basically what my function does is to expand the ...