Tag Info

Hot answers tagged

13

Here's a way with v10.1: strings = {"a", "b", "c", "d", "e"}; StringSplit@StringRiffle[strings, "1 1"] {"a1", "1b1", "1c1", "1d1", "1e"} And if you're not on 10.1 (and as suggested by @ciao), you can achieve the same thing with: StringSplit@StringJoin@Riffle[strings, "1 1"] And just for the heck of it with MapIndexed: MapIndexed[If[First@#2 == ...


13

Patterns get confusing quickly. If you name a pattern you're imposing more restrictions on that pattern that are sometimes difficult to follow. Using your example, s = "1 2 "; StringCases[s, (n : NumberString ~~ " ") .. ] StringCases[s, (NumberString ~~ " ") .. ] In the first case you're telling string cases to match n, where n must be a number string and ...


7

Flatten[ {#[[1]] <> "1", StringInsert[#[[2 ;; -2]], "1", {1, -1}], "1" <> #[[-1]]}] &@strings About 2X to over 10X as fast (depending on string lengths) as Riffle et al. that I noted in comment, and unlike that this will return correct results if strings contain spaces. About 4X as fast as mapping (all tested on MMA 9).


7

You can test whether braces are balanced with something like: ClearAll[balancedBracesQ] balancedBracesQ[str_String] := StringCount[str, "{"] - StringCount[str, "}"] === 0 And use it in string pattern in following way: StringCases[ "a\\newcommand{ABC{asdas}{asdsad}}{DEF{x}y}asdkashdkj\ ...


7

I think you want something like one of these: StringSplit[{"A1", "AB1", "AB12"}, d : DigitCharacter .. :> d] StringSplit[{"A1", "AB1", "AB12"}, d__?DigitQ :> d] Both return {{"A", "1"}, {"AB", "1"}, {"AB", "12"}}. DigitCharacter should be faster as it converts to a regular expression internally whereas DigitQ does not: ...


6

As of version 10.1, Mathematica does not support characters beyond the basic multilingual pane. John Fultz of WRI stated this explicitly as a comment on this question, which asks how to access supplementary CJK ideographs outside of Plane 0. Some of the issues, and partial workarounds, are discussed on StackOverflow here and here.


5

You also need InputForm if you want to convert π to Pi. If you want parentheses, then 2 π/5 // ToString[#, InputForm] & // StringReplace[#, "/" :> "--"] & "(2*Pi)--5" or else 2 π/5 // ToString[#, InputForm] & // StringReplace[#, {"/" :> "--", "(" :> "", ")" :> ""}] & "2*Pi--5"


5

Internal`StringToDouble seems applicable: Internal`StringToDouble /@ {"F2.5", "2.5F"} {2.5, 2.5} The evaluated form of Quantity["2.3s"] is Quantity[2.3`,"Seconds"] therefore as already commented: Quantity["2.3s"][[1]] 2.3 System`Convert`TableDump`ParseTable provides configurable options for processing strings.


5

StringDrop["F2.5", 1] // ToExpression StringDrop["2.5F", -1] // ToExpression and List @@ Quantity["2.3s"] // First or rather as @thils proposed, directly Quantity["2.3s"] // First


5

Pedestrianly, x = {"a", "b", "c", "d"}; Flatten[{StringJoin[#, "1"] &@First[x], StringJoin["1", #, "1"] & /@ x[[2 ;; -2]], StringJoin["1", #] &@Last[x]}] {"a1", "1b1", "1c1", "1d"}


4

As commented, StringTrim for example will do the job. For example : StringTrim[s3] StringTrim[s3, StartOfString ~~ WhitespaceCharacter ..] StringTrim[s3, WhitespaceCharacter .. ~~ EndOfString] return "jbjkbasd nklnkln" "jbjkbasd nklnkln " " jbjkbasd nklnkln"


4

Here is a formulation using regular expression groups and POSIX character classes. transformRules = (# -> First @ StringCases[ToString[#], RegularExpression["([[:alpha:]]+)([[:digit:]]+)"] :> Subscript[Style["$1", Italic], ToExpression["$2"]]] &) transformRules /@ {A1, AB1, AB12} The raw input form for ...


4

To remove line breaks from a string with StringDelete, you can simply do this: string = "I have imported string data, which appears to have line breaks or return codes in it invisible in a notebook input cell. Copying a string and pasting into Apple Pages with control codes showing gets me this:"; StringDelete[string, "\n" | "\r"] (* ==> "I have ...


3

I had another answer to this question (now located here) that used the global variable $PreRead to convert raw strings to InputForm prior to evaluation. Unfortunately, that method only works when evaluating the package file interactively, as you would a notebook file. It does not work when loading a package with Get, because $PreRead is apparently not ...


3

We can implement a raw string syntax using the global variable $PreRead, which "is applied to the text or box form of every input expression before it is fed to the Wolfram Language." Using $PreRead, we can get at the string before any interpretation has been performed on it, replace it with the corresponding InputForm expression, and send that along as ...


3

This issue is due to the same type-inferencing problem described here. Using printSignatures from the referenced answer, we can see that the type inferencer will only accept a single string as the second argument, not a list: printSignatures[StringSplit] (* {Vector[Atom[String], n_]} {Atom[String]} {Atom[String], Atom[String]} ...


3

This may be more robust, assuming you want to see the specific letter "F": ToExpression@StringCases[ #, {"F" ~~ s : NumberString -> s, s : NumberString ~~ "F" -> s}] & /@ {"F3.14", "2.718F","an embedded number F2.34 in a string"} {{3.14}, {2.718}, {2.34}}


3

If I'm not mistaken, an approach like this may work for you: clean = { "\t" -> "\\t", "\b" -> "\\b", "\n" -> "\\n"}; StringReplace["\begin{5}", clean] If too many exceptions appear it may be easier to set up something with FromCharacterCode. Interesting, I found that InputForm auto-strings its argument, like a string version of Hold: ...


3

There is no direct equivalent to Python's r. So from the comments it's clear that we have to cheat in order to get this formatting to work purely in a text file environment. One obvious way to do it is as follows: toString[x_]:=StringReplace[x,""->"\\"]; s = toString["documentclass[12pt,titlepage]{article} ...


3

This is a minor variation on the last example in the Examples > Generalizations & Extensions section of RegularExpression, StringCases["big bad wolf", RegularExpression["(.*) .* (.*)"] -> {"$1", "$2"}] {{"big", "wolf"}} Does this work for you?


2

It seems to be a problem of your specific version. Im running 10.0.1.0 MacOS and it works just fine.


1

I suppose that the set of answers won't be complete without the contribution of a cryptic RegularExpression... $pattern = RegularExpression["\\\\newcommand({([^{}]|(?1))*})(?1)"]; The pattern works for simple cases: StringCases[#, $pattern]& @ "XXX\\newcommand{ABC}{DEF}XXX\\newcommand{GHI}{JKL}XXX\\newcommand{MNO}{PQR}" (* { ...


1

As already noted for Q2 the best solution is of course StringTrim. For Q1 here is an alternative using regular expressions: stringTrimAtEnd = StringReplace[#,RegularExpression["\\s*$"] :> ""] &


1

Q1 Column[StringReplace[#, " " .. ~~ EndOfString -> ""] & /@ {s1, s2, s3}] dasbdk asdnkal asn knkl nkn dvklsn jbjkbasd nklnkln Q2 Column[StringTrim[#] & /@ {s1, s2, s3}] dasbdk asdnkal asn knkl nkn dvklsn jbjkbasd nklnkln


1

I am somewhat confused about the aim. Here is an interpretation. Starting with string: string = "{{13,{17,4},{23,10},220/13,3},{17,{23,6},{29,12},4368/17,3},\ {19,{23,6},{29,12},4368/19,3},{23,{29,6},{41,18},26334/23,6},{29,{41,\ 12},{47,18},21474180/29,3},{29,{41,10},{53,22},493350,6},{31,{41,12},{\ ...


1

I think you might be looking for a pattern structure like this: str = "big bad wolf"; StringCases[str, "big " ~~ x__ ~~ " wolf" -> x] which returns bad or whatever happens to lie between big and wolf. In the event that there is no match, you get a null {}.



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