Tag Info

0

I am not sure I understand the constraints of this question if there are any. But would this work: Map[ Function[ DateString[DatePlus[ DateList[{ToExpression[StringTake[ToString[#1], 4]], 1, 1}], {(ToExpression[StringTake[ToString[#1], -3]] - 1), "Day"}], {"Year", "Month", "Day"}]], dateslalala]

3

Perhaps, using: dateslalala={2003364, 2003157, 2003314, 2003302, 2003181, 2003062, 2003254, \ 2003070, 2003365, 2003338, 2003233, 2003073, 2003020, 2003010, \ 2003238, 2003107, 2003310, 2003347, 2003204, 2003066, 2005364, \ 2005157, 2005314, 2005302, 2005181, 2005062, 2005254, 2005070, \ 2005365, 2005338, 2005233, 2005073, 2005020, 2005010, 2005238, \ ...

1

The thought is DatePlus. split[x_Integer] := {{FromDigits@#[[1 ;; 4]], 1, 1}, FromDigits@#[[5 ;; -1]]} &@IntegerDigits[x] split[2003305] (* {{2003,1,1},305} *) DatePlus[split[2003305][[1]], 305] (* {2003,11,2} *) f = Block[{\$DateStringFormat = {"Year", "Month", "Day"}, res}, res = split[#]; DatePlus[res[[1]], res[[2]] - 1]] ...

0

fn[args__] := Replace[ StringForm[args], x : Except[_String] :> ToString[x, InputForm], 1 ] // ToString Test: fn["SomeText= as well as OtherText=.", 2.0*10^(-15), 2/5] "SomeText=2.*^-15 as well as OtherText=2/5."

2

lst = {"1", "2", "3", "4", "5", "6"}; DeveloperPartitionMap[StringTrim[ToString@#, "{" | "}"] &, lst, 3] (* {"1, 2, 3", "4, 5, 6"} *)

1

This is very similar to your previous question and so is the solution: lst = {"1", "2", "3", "4", "5", "6"} ToString @ Row[#, ", "] & /@ Partition[lst, 3] {"1, 2, 3", "4, 5, 6"}

1

This gives exactly the output you wrote down: str1 = StringDrop[StringDrop[ToString@Partition[lst, 3][[1]], 1], -1] str2 = StringDrop[StringDrop[ToString@Partition[lst, 3][[2]], 1], -1] Could be made more elegant of course, and more flexible.

1

Assuming you always want strings with 3 items: Map[StringJoin @@ Riffle[#, ", "] &, Partition[Map[ToString, lst], 3]]

2

StringTake[ToString@#, {2, -2}] & /@ Partition[lst, 3] {"1, 2, 3", "4, 5, 6"}

1

Your output is formatted wrongly, because what you gave to Style is not a series of things to style, but a multiplication of its elements. Just evaluate the arguments on their own in your notebook to see the effect: "MLS" Subscript[u, 2] OverTilde[\[Eta]] "=0.048%" (* "=0.048%" "MLS" \!$$\*OverscriptBox[\(\[Eta]$$, $$~$$]\) Subscript[u, 2] *) To get what ...

2

You can try using Row to layout the text, and use Spacer command to adjust the spacing as needed Plot[Sin[x], {x, -Pi, Pi}, PlotLabel -> Text@Style[ TraditionalForm[ Row[{"MLS", Spacer[5], Subscript[u, 2], Spacer[20], OverTilde[\[Eta]] , Spacer[5], "= 0.048%"}]], FontSize -> 18], ImagePadding -> 20] Or, the way I would ...

2

InputForm[ ToString@StringForm["SomeText= as well as OtherText=.", "textA", "textB"]] "SomeText=textA as well as OtherText=textB." If you have version 10 you might want to try StringTemplate StringTemplate["SomeText= as well as OtherText=`."]["textA", "textB"] "SomeText=textA as well as OtherText=textB."

1

Terse: a = {1, 17, 2/3, 4/5, 9/7, 3/7, 1/7, 1/9}; ToString @ Row[InputForm /@ a, " "] "1 17 2/3 4/5 9/7 3/7 1/7 1/9"

2

Or StringTake[ToString[a, FormatType -> InputForm], {2, -2}] The inelegant use of StringTake strips off the leading and trailing brackets.

4

I would love this to be uniform for both integer and rational numbers a2 = {2/3, 4/5, 9/7, 3/7, 1.5, 3, 1/9}; StringTrim@StringJoin[" " <> ToString[#, InputForm] & /@ a2] (* 2/3 4/5 9/7 3/7 1.5 3 1/9 *) Row[ToString[#, InputForm] & /@ a2, " "] (* 2/3 4/5 9/7 3/7 1.5 3 1/9 *) StringReplace[ToString[a, InputForm], {"{" | "}" -> ...

1

a = {2/3, 4/5, 9/7, 3/7, 1/7, 1/9}; StringJoin@Cases[a, Rational[x_, y_] :> " "<>ToString[x] <> "/" <> ToString[y]] reply to comment: a = {2/3, 4/5, 9/7, 3/7, 1/7, 1/9, 5, 6, 99/10}; f[Rational[x_, y_]] := " " <> ToString[x] <> "/" <> ToString[y]; f[x_] := " " <> ToString[x]; StringJoin[f[#] & /@ a] ...

5

I am not sure if this is the best way of doing it, so the following is not exactly an answer to the question. Here is how I would do it assuming that I have padding. 0 Preparation data={1,2,3};(*some test data*) (*exporting to some files*) Table[Export["myData" <> IntegerString[i, 10, 5] <> ".txt", data], {i,10}]; (*and deleting one to make it ...

1

Pick[ln, Tr /@ StringPosition[nm, #], 1] &@"dow" Pick[ln, Tr /@ StringPosition[nm, #], 1] &@{"dow", _ ~~ "at"} Let's you use single and lists of targets, patterns (unlike straight pick)... assumptions about list correspondence apply, returns results in order of nm.

0

MapAt[ToExpression@ToString@# &, #, Position[#, _Rule]] &[ToExpression@#] &@str (* {1 -> {a -> aa, b -> bb}, 2 -> {2 a -> {b -> bb}}} *)

6

Another option: AssociationThread[nm -> ln]["tat"] If you store AssociationThread[lm -> ln] in a symbol you can use it for many quick lookups without having to recreate the association every time. Without Pick and AssociationThread you might do something like Identity @@ Cases[Transpose[{nm, ln}], {"tat", v_} :> v] Note that I'm using ...

10

if the keys are unique, then this seems to be rather elegant, short and still clear: Pick[ln, nm, "tat"] probably look at the documentation of Pick...

3

How about this? SelectFirst[ln, StringMatchQ[#, "tat" ~~ __] &] Admittedly my method is not as elegant as others, but it's fast if your ln and nm are large: ln = ConstantArray[t, 10^5] /. t :> StringJoin@RandomSample[CharacterRange["A", "z"], 10]; nm = StringTake[#, 3] & /@ ln; The following Timing[SelectFirst[ln, StringMatchQ[#, nm[[1]] ~~ ...

2

You can start by simply creating a function that tests, whether a string is a list of rules or not isTransformable[str_String] := SyntaxQ[str] && MatchQ[MakeExpression[str], HoldComplete[{_Rule ..}]]; isTransformable[___] := False; Note that this function does much more that search for a "->" inside a string. First, it tests, whether the ...

1

I don't quite understand your code, especially the key_/;key->val_ part, but the following code shall do your work: ToExpression@str //. key_String /; StringMatchQ[key, ___ ~~ "->" ~~ ___] :> ToExpression[key]

14

This is a bug in version 10.1.0. We decided it was serious enough to warrant a fix via an automatic paclet update. The paclet has been pushed live and Mathematica should install it automatically once it does a periodic check with the paclet server. It should take about a week or so. To install it right away, you can do PacletInstall["StringPatternFix"]. You ...

0

"Xabcde" /. a_ :> StringCases[a, _] /. {"X", b__} :> StringJoin[b] (*"abcde"*)

4

Use StringReplace["Xabcde", "X" ~~ e__ -> e]. Replace, et al are for lists/expressions... Notice that AtomQ@"Xabcde" is True, so regular (non-string) replace operations only "see" it as a singular entity: "Xabcde" /. "Xabcde" -> 1 (* 1 *) From the docs for ReplaceAll: "... to transform each subpart..." - but there is no "subpart" for atoms, so ...

3

For performance fiends: I had an application that had this very need for some huge sets of long integer strings. kguler's solution is certainly the canonical way, and quite quick, as is Felix's version (I was actually surprised on that one). eldo's solution is probably what most would come up with, but in performance-intensive scenarios the StringSplit is ...

0

As of 10.1 this is built in to Mathematica with StringPadLeft: StringPadLeft[#,2,"0"]&@*ToString/@{-6,1,3,23} {"-6", "01", "03", "23"}

1

Or the StringReplace version: In[3]:= StringReplace[str, WordCharacter .. ~~ " -> ," -> ""] Out[3]= "{Aaaa -> a, Bbbb -> b, Ddddd -> c, Fffff -> e}" In[4]:= ToExpression@% Out[4]= {Aaaa -> a, Bbbb -> b, Ddddd -> c, Fffff -> e}

2

FromDigits /@ StringSplit["1 2 3 4 5 6"] ToExpression@StringSplit["1 2 3 4 5 6"] StringCases["1 2 3 4 5 6", ns : NumberString :> FromDigits[ns]] ToExpression@StringCases["1 2 3 4 5 6", NumberString] all give (* {1, 2, 3, 4, 5, 6} *)

2

res = ToExpression@StringSplit["1 2 3 4 5 6", " "] {1, 2, 3, 4, 5, 6} Head /@ res {Integer, Integer, Integer, Integer, Integer, Integer}

2

Flatten[ImportString["1 2 3 4 5 6", "Table"]] {1,2,3,4,5,6} Head /@ % {Integer, Integer, Integer, Integer, Integer, Integer}

1

ToExpression["{" <> StringReplace["1 2 3 4 5 6", " " -> ","] <> "}"]

2

You can also use a combination of StringSplit, SyntaxQ and Pick: str = "{Aaaa -> a, Bbbb- > b, Cccc -> , Ddddd -> c, Eeeee -> , Fffff -> e}"; str2 = Pick[#, SyntaxQ /@ #] &@StringSplit[str, "," | "{" | "}"] (* {"Aaaa -> a", " Ddddd -> c", " Fffff -> e"} *) ToExpression@str2 (* {Aaaa -> a, Ddddd -> c, Fffff -> e} *) ...

2

You can use StringCases. str = "{Aaaa -> a, Bbbb -> b, Cccc -> , Ddddd -> c, Eeeee -> , Fffff -> e}"; ToExpression@StringCases[str, WordCharacter .. ~~ " -> " ~~ WordCharacter ..] {Aaaa -> a, Bbbb -> b, Ddddd -> c, Fffff -> e}

4

I thought it worth mentioning that the documentation for StringMatchQ gives the following solution to your problem: Verbatim["p"] specifies the verbatim string "p", with * and @ treated literally. So you'd be able to do something like: StringMatchQ["*", Verbatim["*"]] (* True *)

2

My main goal here is to provide an example for other people who want to experiment with LibraryLink and strings, as well as to test how fast all of this is (and to become the "bottom line" in the fancy plot of course >:D ). Anyway I made the following functions in C. Note that you have to have a C-compiler set up in such a way that Mathematica knows about ...

6

Workaround for the two-argument Except in string patterns issue until it is fixed: StringCases["104702", DigitCharacter?(! StringMatchQ[#, "0"] &)] Match the second argument directly, then use PatternTest to check that it also doesn't match the first argument.

3

fixed in 10.1 (windows): code: StringCases["abcadcacb", "a" ~~ x_ ~~ "c"] StringCases["a" ~~ x_ ~~ "c"]["abcadcacb"]

1

f = StringMatchQ[#, NumberString ~~ " " ~~ __] &; f /@ {"156 af", "15.6 af", "a 156 af", "a 1.56 af"} {True, True, False, False} See StringMatchQ, StringExpression, NumberString, BlankSequence, Slot, Function Related: How can I improve NumberString?

1

ToExpression[ "{a-> POR, b-> D610, c-> 0, d-> \"300/7\", e -> \"1/400\"}" ] /. s_String :> Defer @ ToExpression[s] {a -> POR, b -> D610, c -> 0, d -> ToExpression["300/7"], e -> ToExpression["1/400"]} response to comment: ToExpression[ "{a-> POR, b-> D610, c-> 0, d-> \"300/7\", e -> ...

3

Perhaps the closest thing to what you want is to use HoldForm to prevent the sorting behavior of Times due to Orderless: str = "one Test String to see"; HoldForm @@ MakeExpression @ str one Test String to see However it is important to realize that the HoldForm head is still present, only that it is not shown in standard formatted output. Also know ...

2

Just because I wanted to join the party and with no redeeming features (...just like playing with Reap and Sow): f[str_] := Module[{ss = StringSplit[str, ""], a, b, rul}, {a, b} = Reap[MapIndexed[Sow[w[First@#2, #1], DigitQ[#1]] &, ss]]; rul = Join[Thread[b[[1]] -> Sort[b[[1, All, 2]]]], Thread[b[[2]] -> b[[2, All, 2]]]]; StringJoin[a ...

3

strng = "95uge678r3gi89hgfe30kgh063d51"; Block[{c = Sort@StringCases[#, DigitCharacter], j = 0}, StringReplace[#, DigitCharacter :> c[[++j]]]] &@strng (* "00uge133r3gi55hgfe66kgh788d99" *)

10

I think you'll find this much faster than answers so far... strnumsrt = Module[{tc = ToCharacterCode[#], tcc, tcr}, tcc = Unitize@Clip[tc, {48, 57}, {0, 0}]; tcr = Pick[Range@Length@tcc, tcc, 1]; tc[[tcr]] = Sort[tc[[tcr]]]; FromCharacterCode[tc]] &;

9

StringReplacePart gets very slow when there are many replacements. A more direct approach proves to have far better complexity. My proposal: subSort[s_String] := Module[{p, ch}, p = StringPosition[s, DigitCharacter]; If[p === {}, Return[s], p = p[[All, 1]]]; ch = Characters[s]; ch[[p]] = Sort @ ch[[p]]; StringJoin[ch] ] rasher took this idea ...

4

This is known to be inefficient because using patterns like this gives a poor complexity, but it works: str = "95uge678r3gi89hgfe30kgh063d51"; FixedPoint[StringReplace[ #, a___ ~~ b : DigitCharacter ~~ c___ ~~ d : DigitCharacter ~~ e___ /; !OrderedQ[{b, d}] :> StringJoin[{a, d, c, b, e}] ] &, str] (Thanks to Simon Woods for ...

6

This is a slight rewrite of the code in the question, rather than anything elegant or clever. You can use the DigitCharacter pattern in StringPosition instead of ToString /@ Range[0, 9], and having found the positions you can use them in StringTake instead of searching the string again with StringCases: With[{p = StringPosition[#, DigitCharacter]}, ...

Top 50 recent answers are included