Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
data4 = {{"050318", "111529"}}

 ID2 = data4[[i, 2]];
 s = Import["" <> ID2, "Text"];
 s = StringSplit[s, "\n"];
 line = Position[s, "Flux Light Curve"],
 {i, 1, Length[data4]}]

The StringSplit works properly, but when I want to identify the position it gives line {} empty I expect instead a number like {{106}} for example

Even when I try with

 ID2 = data4[[i, 2]];
 s = Import["" <> ID2, "Text"];
 s = StringSplit[s, "\n"];
 line = StringPosition[s, "Flux Light Curve"],
  {i, 1, Length[data4]}]

it gives me not a single line, but many empty ones.

share|improve this question

The string "Flux Light Curve" does not appear as an element of s. If you are looking for a substring you will need a different function.

If you only want to know the positions of strings which contain "Flux Light Curve" as a substring, you might use:

Position[ s, x_ /; StringMatchQ[x, __ ~~ "Flux Light Curve" ~~ __] ]

In this ~~ stands for StringExpression.

The output from this:

StringPosition[s, "Flux Light Curve"]

is a list with many empty lists as elements, because most elements in s do not contain the substring. You can further process the result as needed. For example:

x = StringPosition[s, "Flux Light Curve"];

Position[x, {__Integer}]

DeleteCases[x, {}]
share|improve this answer

Mr.Wizard's answer correctly identifies why your code wasn't working: you needed StringPosition rather than Position, and then to delete the empty cases.

I wonder if there isn't another way to attack your problem. You are importing an HTML file, so why not use that fact rather than treat it as plain text? You don't actually need all the HTML markup. So instead consider:

ss = Import["", "HTML"];

This strips out the extraneous HTML stuff. So the first 100 characters are:

StringTake[ss, 100]
"Skip banner and navigation tools .  

   Dept. of Physics & Astronomy  
    US Site | "

Contrast this with

s = Import["", "Text"];
StringTake[s, 100]

enter image description here

(I had to insert this as a picture because inserting an HTML DOCTYPE statement really doesn't seem to work nicely with the StackExchange software, and rightly so!)

As you can see, you can obtain the positions of the desired substring within the whole, without having to split the string first.

StringPosition[ss, "Flux Light Curve"]

{{1797, 1812}, {2435, 2450}, {2465, 2480}}

Of course, you can always StringSplit in the same way and find the substring that contains the sub(sub)string:

sss = StringSplit[ss, "\n"];
StringPosition[sss, "Flux Light Curve"]
{{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {{52, 67}},
   {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, \
   {}, {}, {{1, 16}, {31, 46}}, {}, {}, {}, {}, {}, {}}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.