Categories
discuss

Converting a sentence string to a string array of words in Java

I need my Java program to take a string like:

"This is a sample sentence."

and turn it into a string array like:

{"this","is","a","sample","sentence"}

No periods, or punctuation (preferably). By the way, the string input is always one sentence.

Is there an easy way to do this that I’m not seeing? Or do we really have to search for spaces a lot and create new strings from the areas between the spaces (which are words)?

Answer

String.split() will do most of what you want. You may then need to loop over the words to pull out any punctuation.

For example:

String s = "This is a sample sentence.";
String[] words = s.split("\s+");
for (int i = 0; i < words.length; i++) {
    // You may want to check for a non-word character before blindly
    // performing a replacement
    // It may also be necessary to adjust the character class
    words[i] = words[i].replaceAll("[^\w]", "");
}
Categories
discuss

Convert integer dates/times to unix timestamp in Java?

This mainly applies to android, but could be used in Java. I have these listeners:

int year, month, day, hour, minute;
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                year = year;
                month = monthOfYear;
                day = dayOfMonth;
            }
        };
// the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
    new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            hour = hourOfDay;
            minute = minute;
        }
    };

How could I convert int year, month, day, hour, minute; to a unix timestamp? Is this possible without the Date class?

Answer

Okay, use Calendar then, since that’s preferred to Date anyway:

int componentTimeToTimestamp(int year, int month, int day, int hour, int minute) {

    Calendar c = Calendar.getInstance();
    c.set(Calendar.YEAR, year);
    c.set(Calendar.MONTH, month);
    c.set(Calendar.DAY_OF_MONTH, day);
    c.set(Calendar.HOUR, hour);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);

    return (int) (c.getTimeInMillis() / 1000L);
}

Calendar won’t do any computations until getTimeMillis() is called and is designed to be more efficient than Date.

Categories
discuss

How do I reference a file stored in internal storage from a WebView?

I have some files stored in the app’s “internal storage”, which is located on the device here:

/data/data//files/

Here’s the problem: I want to reference these files from the webview. How do I do this? What should I use as the baseUrl?

I need something similar to file:///android_asset but for “internal storage” instead of for files in the assets directory.

Answer

Try this:

file:///data/data/com.yourproject.example/files/image.png
Categories
discuss

How can I access all drawables in android?

Only some drawables are accessible via the

<ImageButton
src="@+android:drawable/

syntax. For example expander_ic_maximized is not accessible, yet its clearly at
http://androiddrawableexplorer.appspot.com/

its so frustrating to have to download the drawables and manually put them in my drawables folder….any ideas how to get around this???

Answer

Android SDK has all images unpacked in the %SDK-FOLDER%/platforms/android-*/data/res/drawable-* folder. Just pick desired file and put it into your app resource.

Categories
discuss

Cleaner way to write element.parent().parent().parent().parent().parent()

If I need to select 10-th parent, is there a cleaner way, then repeating .parent() 10 times?

$('#element_id').parent().parent().parent().parent().parent().parent().parent().parent().parent().parent();

Answer

If you truly need to get the 10th parent, and you are unable to use a selector to get there, the smoothest way would probably be something like this:

$('#element_id').parents().eq(9);
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..