Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for that? Answer Convert comma separated String to List The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or
Tag: string
How to remove the last character from a string?
I want to remove the last character from a string. I’ve tried doing this: Getting the length of the string – 1 and replacing the last letter with nothing (deleting it), but every time I run the program, it deletes middle letters that are the same as the last letter. For example, the word is “admirer”; after I run the
Gibberish at the end of a String byte array passed to native code
I am writing an applet to wrap a proprietary .dll that can be used in the browser. To achieve this, I am using JNA. The .dll connects to a check scanner peripheral, and can pull images from the devices memory. I have to make a Windows API call in Java, using JNA, to get the image: When the code saves
Splitting strings through regular expressions by punctuation and whitespace etc in java
I have this text file that I read into a Java application and then count the words in it line by line. Right now I am splitting the lines into words by a But I know I am missing out on some words from the text file. For example, the word “can’t” should be divided into two words “can” and
Converting Integer to String with comma for thousands
I want to convert an Integer 35634646 to have the thousand “,” so it should be 35,634,646. What would be the quickest way to doing that? Answer
Strip Leading and Trailing Spaces From Java String
Is there a convenience method to strip any leading or trailing spaces from a Java String? Something like: Result: myString.replace(” “,””) would replace the space between keep and this. Answer You can try the trim() method. Take a look at javadocs
Most efficient way to convert a single char to a CharSequence
What’s the most efficient way to pass a single char to a method expecting a CharSequence? This is what I’ve got: According to the answers given here, this is a sensible way of doing it where the input is a character array. I was wondering if there was a sneaky shortcut I could apply in the single-char case. Answer
Java CharAt() and deleteCharAt() performance
I’ve been wondering about the implementation of charAt function for String/StringBuilder/StringBuffer in java what is the complexity of that ? also what about the deleteCharAt() in StringBuffer/StringBuilder ? Answer For String, StringBuffer, and StringBuilder, charAt() is a constant-time operation. For StringBuffer and StringBuilder, deleteCharAt() is a linear-time operation. StringBuffer and StringBuilder have very similar performance characteristics. The primary difference is
java: save string as gzip file
I’m java beginner, I need something like this: String2GzipFile (String file_content, String file_name) String2GzipFile(“Lorem ipsum dolor sit amet, consectetur adipiscing elit.”, “lorem.txt.gz”) I …
Count words in a string method?
I was wondering how I would write a method to count the number of words in a java string only by using string methods like charAt, length, or substring. Loops and if statements are okay! I really appreciate any help I can get! Thanks! Answer