1

i get a big string from a web page and its like this

"07:10Ο ΑΣΔΦΑΣΔΦ07:30ΑΣΔΦΑΣΔΦ10:15ΝΑΣΔΦΑΣΔΦ"

and i want every time i find time like this "17:50" or "07:30" to get a new line before the time! so i will have

07:10   ΑΣΔΦΑΣΔΦ
07:30   ΑΣΔΦΑΣΔΦ
10:15   ΝΑΣΔΦΑΣΔΦ

etc...

but here is my problem i tried to copmare the letters with this

StringBuilder builder = new StringBuilder(text);
for (int i = 0; i < text.length(); i++) {
     if(Character.isDigit(text.charAt(i))){
            builder.insert(i, "\n");
     }
}

but inside my text i have Greek letters so the isDigit returns 1 when it gets some Greek letters. Anyone has any idea how to solve this?

5
  • 2
    Please clean up your question. Your example has no Greek letters, which you claim is a source of your problem. The strings "00:23" "12:45" occur 0 times in your example string. Your sample output appears to be unrelated to your sample input. Is there a meaningful difference between "06:45" and "06.45"? I notice a "56:65" in your sample input. Is "99:99" a valid time? What about "11111.99"? If you can clarify/clean this up, I can probably help. Commented Dec 2, 2011 at 17:23
  • @ccoakley this was just an example, ok i quoted the original text to see how it is. Commented Dec 2, 2011 at 17:27
  • @AlexanderFragotsis: I have started a regex version of a solution, but I'm not sure if this is the way you want to go. If it is, I can flesh it out. Commented Dec 2, 2011 at 17:31
  • yes i dont mind i just dont know how to do this. Can you post me a link with a tutorial or something? Commented Dec 2, 2011 at 17:34
  • @Alexander Fragotsis: I have change my answer, now it works -- proved. Commented Dec 2, 2011 at 18:18

4 Answers 4

2

To provide some initial help, the builder.insert() line isn't doing what you probably want:

StringBuilder builder = new StringBuilder(text);
for (int i = 0; i < text.length(); i++) {
     if(Character.isDigit(text.charAt(i))){
            builder.insert(i, "\n"); // questionable
     }
}

Do you really want `\n1\n2:\n4\n5" for the string "12:45"?

You might just want to match "\\d\\d[:.]\\d\\d" and prepend "\n" postpend " " on each match, but I can't be certain I understand your problem.

Specifically, I just tried:

String str = "07:10Ο Σκούμπι Ντου & ο κολλητός του07:30Πρωϊνή μελέτη10:15Νηστικοί πράκτορες11:15Σαρίτα, είσαι η ζωή μου12:50Οι ειδήσεις του Star13:45Made in Star15:45Μίλα17:45Ειδήσεις17:50Φώτης - Μαρία live19:45Οι ειδήσεις του Star21:00Ο Χαρί Πότερ και ο ημίαιμος πρίγκιψ00:15Σχολή για απατεώνες01:15Supernatural02:15Gypsy woman02:30Τα πλοκάμια του τρόμου03:45Ραπ πάρτι04:30The Dead zone";
return str.replaceAll("(\\d\\d[:.]\\d\\d)", "\n$1 ");

Does this work the way you want? My terminal doesn't seem to support these characters very well, so I may have a locale encoding issue.

Sign up to request clarification or add additional context in comments.

6 Comments

yea i know first i tried for one digit only but i got this problem and i didnt resumed. i want "\n12:45"
Are you willing to use a regex?
@AlexanderFragotsis: google for java regex tutorial and pick the one you find nicest. I don't really have a suggestion. However, if I understand your problem, I may have something (see my edit). That works on times in both the "12:45" and "12.45" format. I wasn't sure if that was intentional in your initial post or not.
return str.replaceAll("(\\d\\d[:.]\\d\\d)", "\n$1 "); worked perfect! thank you very much! I thought replace all will just replace the values and they wil not exist any more! thank you
if you don't want "." in your times, use the regex "(\\d\\d:\\d\\d)". No need in doing a character class [:.] if you don't want it. But seriously, google for a regex tutorial. It will save you many headaches in the future. Full disclosure: regex will also cause many headaches in the future.
|
1

The problem is not the isDigit detection, the problem is builder.insert(i, "\n");!

try this, it works without insert:

@org.junit.Test
public void endodingTest() {
    String text = "07:10Ο Σκούμπι Ντου & ο κολλητός....";
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < text.length(); i++) {
         char c = text.charAt(i);
         if(Character.isDigit(c)) {
             builder.append("\n");
         }
         builder.append(c);

    }
    System.out.println(builder.toString());
}

The problem is, that everytime you insert a additional \n in the String builder, every char after that line break becomes moved one char backwards. To correct this you have to count all the linebreakes you already inserted and if you insert a new one you have to insert it at position i + numberOfAlreadyInsertedLineBreaks

(builder.insert(i + numberOfAlreadyInsertedLineBreaks, "\n");) complete example below)


the second thing of course (but you already know it) is that you have to improve your pattern, so at the end this is the soultion

@org.junit.Test
public void endodingTest() {
    String text = "07:10Ο Σκούμπι Ντου & ο κολλητός του07:30Πρωϊνή μελέτη10:15Νηστικοί πράκτορες11:15Σαρίτα, είσαι η ζωή μου12:50Οι ειδήσεις του Star13:45Made in Star15:45Μίλα17:45Ειδήσεις17:50Φώτης - Μαρία live19:45Οι ειδήσεις του Star21:00Ο Χαρί Πότερ και ο ημίαιμος πρίγκιψ00:15Σχολή για απατεώνες01:15Supernatural";
    StringBuilder builder = new StringBuilder(text);
    int numberOfAlreadyInsertedLineBreaks = 0;
    for (int i = 0; i < text.length(); i++) {

        if (match(text, i)) {
            builder.insert(i + numberOfAlreadyInsertedLineBreaks, '\n');
            numberOfAlreadyInsertedLineBreaks++;
        }

    }
    System.out.println(builder.toString());
}

private boolean match(String text, int i) {
    return Character.isDigit(text.charAt(i))
            && Character.isDigit(text.charAt(i + 1))
            && text.charAt(i + 2) == ':'
            && Character.isDigit(text.charAt(i + 3))
            && Character.isDigit(text.charAt(i + 4));
}

1 Comment

yes apparently after the insert(i, "\n") the value of i wasnt the same as before so i got errors.
0

First, I am afraid you are wrong. I took Greek ABC from Wikipedia and ran the following loop on it:

public static void main(String[] args) {
    System.out.println("before");
    String greek = "ΑαΒβΓγΔδΕεΖζΗηΘθΙιΚκΛλΜμΝνΞξΟοΠπΡρΣσςΤτΥυΦφΧχΨψΩω";
    for (char c : greek.toCharArray()) {
        if (Character.isDigit(c)) {
            System.out.println("digit is found: " + c);
        }
    }
    System.out.println("after");
}

It printed:

before
after

This means that isDigit() works correctly.

Concerning to extracting time from the string. I'd recommend you to use the following regex:

    Pattern p = Pattern.compile("(\\d{2}:\\d{2})");
    Matcher m = p.matcher(str);
    int start = 0;
    while(m.find(start)) {
        String time = m.group(1);
        start = m.end();
                // time variable contain time HH:mm. Just use it as you need
    }

Comments

0

Try Pattern:

scala> java.util.regex.Pattern.compile("(\\d\\d.\\d\\d)").matcher("first12.34second56.78third90.12fourth34.56").replaceAll("$1 ")
res1: java.lang.String = "first12.34 second56.78 third90.12 fourth34.56 "


scala> java.util.regex.Pattern.compile("(\\d\\d.\\d\\d)").matcher("αβγδεζηθικλ12.34αβγδεζηθικλ56.78αβγδεζηθικλ90.12αβγδεζηθικλ34.56").replaceAll("$1 ")
res2: java.lang.String = "αβγδεζηθικλ12.34 αβγδεζηθικλ56.78 αβγδεζηθικλ90.12 αβγδεζηθικλ34.56 "

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.