New answers tagged

4

str = StringReplace["{ab{c{de}}f}",x : LetterCharacter :> "," <> x <> ","]; Quiet[ToExpression[str] /. Null :> Nothing] (*{a, b, {c, {d, e}}, f})*


9

Block[{Times = Sequence}, ToExpression@ StringRiffle@ Characters@ "{ab{c{de}}f}" ] (* {a, b, {c, {d, e}}, f} *)


6

str = "{ab{c{de}}f}"; str2 = StringReplace[str, x : LetterCharacter ~~ y : LetterCharacter :> x ~~ "," ~~ y]; Block[{Times = Sequence}, ToExpression[str2]] {a, b, {c, {d, e}}, f}


4

StringTake and Span would be useful. For example, to get the second characters: StringTake[s, 2 ;; ;; 21] (* "LLFOWJZSPLJJHNHQOQLYSOPWQOSZNTTLTOHETNJOJOODOCJFQOJ" *) To get all of the strings: StringTake[s, Array[# ;; ;; 21&, 21]] Another approach would be a combination of Characters and Part: StringJoin[Characters[s][[2 ;; ;; 21]]] (* ...


2

You can also use FromDigits: FromCharacterCode[FromDigits[#, 16] & /@ StringSplit["\u5929\u4e0a\u4eba\u95f4", "\u"]]


5

I'm going to assume you want this specific char: Unicode Han Character The issue is not with Mathematica's character code conversion, but instead the encoding source. The value you have obtained is in Hex, where you need the decimal to use FromCharacterCode. Hex-> Dec-> Front end FromCharacterCode[Interpreter["HexInteger"]["5929"]] References ...


7

Here is an example of formating the srt file. Since it is a text file, it can be exported using the "String" option: exportRST[path_, string_] := Module[{formateLine, strLs,}, formateLine[str_] := Module[{timePos, times, text}, timePos = StringPosition[str, _ ~~ ":" ~~ _ ~~ _ ~~ "." ~~ _ ~~ _]; times = StringTake[str, #] & /@ timePos; text ...


4

You can get this result by mixing StringExpression with RegularExpression. Regular expressions have the ability to perform a negative look-ahead. This can be used to insure that no numbers which have a decimal followed numbers are matched. getIntegers[s_String] := StringCases[ s, (StartOfString | Whitespace) ~~ u : Repeated["-", {0, 1}] ~~ n : ...


7

With both StringPattern and RegularExpression the problem is greediness: wildcards will try to match as much as possible. With StringPattern this can be fixed using Shortest: StringReplace[buf, "\\text{" ~~ Shortest[x___] ~~ "}" :> x] With a regular expression a quantifier can be made ungreed with ? (e.g. {(.*?)}), but when you're going that way, you ...


3

If on the off chance you are seeking binary output for numbers then: dat = {{M, 1, 0, 0, 1, 1, 1, 0}, {A, 0, 0, 0}}; {First@#, BaseForm[FromDigits[Rest@#, 2], 2]} & /@ dat Hope this helps.


3

Transpose[{a[[;; , 1]], StringJoin @@@ IntegerString @ a[[;; , 2 ;;]]}]


7

lis = {{M, 1, 0, 0, 1, 1, 1, 0}, {A, 0, 0, 0}}; {First@#, StringJoin[ToString /@ Rest@#]} & /@ lis (* {{M, "1001110"}, {A, "000"}} *)


8

Cases[{{M, 1, 0, 0, 1, 1, 1, 0}, {A, 0, 0, 0}}, {x_, y__} :> {x, StringJoin[ToString /@ {y}]}]



Top 50 recent answers are included