Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I have 'R-121|123|123' this type of string.
Now I want to split the above string in two sub string.
For example : Two sub string after splitting R and 121|123|123
I can split easily by using '-' this operator.
We can also do the same thing by using substring operator.
Which one is better (split or substring) ?
Split goes through your entire string, and creates new objects plus the array object itself,
So substring would definitely be more efficient
this may be useful :
http://www.gettingcirrius.com/2010/11/performance-comparison-string-split-vs.html
Split is more clean way. If data set is small split can be used.
Regarding performance i am not very clear how substring will be better than split.
I have also read that substring is better but i am not convinced because in case of substring also string traversal is required to find index. so traversing time should be same. But yes split will create all new objects based on delimiter, which will be garbage collected. So as per my understanding, if requirement is to get all the tokens then use split but if specific occurrence is needed then better to go for substring.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to remove prefix and suffix in a String and extract the middle portion of the string.
For eg: Consider the Strings - "www.hello.com" and "www.test.com"
Here prefix - "www." and suffix - ".com". I want to extract the middle words - hello and test.
Currently i have achieved this using String.replace() method in Java.
str.replace("www.","").replace(".com","");
I want know is there any regular expression available to achieve it in a single method in java.
You could use a regex for that, it would work in the same way. Your regex would simply contain a capture group with both the prefix and the suffix in an OR operation.
(www\.|\.com)
You could then use this like you did with the replace.
String test = "www.test.com";
String output = test.replaceAll("(www\\.|\\.com)","")
P.S. this code is untested. Please don't just copy and paste it expecting everything to work.
(?<=www.)(.*)(?=.com)
This uses the lookbehind and lookahead feature of regex
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have CSV file which contains following line.
INPUT:
No,NAme,ID,Description
1,Stack,232,"ABCDEFGHIJKLMNO
-- Jiuaslkm asdasdasd"
2,Queue,454,"PQRSTUVWXYZ
-- Other
words here"
3,Que,4343,"sdfwerrew"
OUTPUT EXPECTED:
No,NAme,ID,Description
1,Stack,232,"ABCDEFGHIJKLMNO \n -- Jiuaslkm asdasdasd"
2,Queue,454,"PQRSTUVWXYZ \n -- Other \n words here"
3,Que,4343,"sdfwerrew"
or
No,NAme,ID,Description
1,Stack,232,"ABCDEFGHIJKLMNO -- Jiuaslkm asdasdasd"
2,Queue,454,"PQRSTUVWXYZ -- Other words here"
3,Que,4343,"sdfwerrew"
Is there any java regex pattern available to find and merge the lines based starting double quotes and end quotes?
You are going down the wrong path. Not everything should be solved using regular expressions. CSV parsing is one of those things.
Seriously: you are about to re-invent the wheel. And the wheel you are about to create will be deficient, and prone to break over and over again.
The sane approach: there are many existing CSV parsers for Java out there. They deal perfectly with multi-line values. So: use one of them (see here as starting point for the many choices you have)
There is a nice rule of thumb: when your regex becomes so complicated that you can't write it down yourself; then consider doing things differently. You are the person who owns this code; you will have to maintain and maybe enhance it - not those folks here that are able to write down a regex that solves this one flavor of CSV example input.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to be able to encode any string into a valid java class name and then decode that class name back into the provided string. I want to be able to do this is a lossless manner, i.e., no two strings can be encoded to the same java class name.
Is this possible?
The answer to this is clearly no. There are only a finite number of possible Java strings, and not all of them are valid class names. Therefore, you're asking for a bijection between two sets of unequal cardinalities - which naturally doesn't exist.
This is certainly possible.
In any situation where you need to convert arbitrary strings to use a limited set of characters, you simply need to invent an escape sequence.
For example, pick _ as your escape character, then replace any invalid character, or any underscore, in the source string with an underscore followed by 8 hex digits of the character's Unicode codepoint.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a list of string (more than 5000 in count) example listed below, these string are separated by an equal "=" delimiter, and left side of delimiter is the string that i need to check with end of another String and if there is an match then i need to replace it with right side of the string.
,.LLC=LLC
D.E.F.=DEF
&,Aida.=AID
&ECho,MA=ECHO
Example:
If string is HelloD.E.F. than it should be replace by HELLO DEF
If string is Hello&ECho,MA than it should be replace by HELLO ECHO
Need to find the most efficient way to get it done. Rather than iterating all the string recursively. (Efficient in time consumption, memory consumption is not a issue.)
There are a number of reasonable ways to do it, depending on your exact requirements. Personally I wouldn't do this in Java at all; I would convert your = delimited patterns into sed replacements, e.g.:
s/,.LLC/LLC/
s/D.E.F./DEF/
s/&,Aida./AID/
s/&ECho,MA/ECHO/
You might have to do some additional escaping depending on the exact strings; e.g. \ and ( are special. Once you have your sed-style expressions simply pass in your input strings and you'll get out your desired strings:
$ sed -f list_of_patterns.txt list_of_strings.txt
If you really need to do this in Java you'll probably want to parse the replacements into a Map<String, String>, then loop over your strings, checking their suffixes one-by-one for matches in your map. Start with the last character, then the last two, last three, and so on. If you find a match you can concatenate the rest of the string (before the matched suffix) with the corresponding value in the map.
This is O(1) on the number of replacements, but O(n) on the number of characters in the strings you need to replace.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm developing an application where the user will insert an vehicle ID.
At our country, the ID is always 3 letters and 4 numbers.
How can I check if a String has X numbers and Y letters in Java?
You can simply work with regular expressions. Something like:
if(vehicleId.matches("^[A-Z]{3}\d{4}$"))
(Assuming the id contains three capital letters followed by four digits.)
This will return a boolean if vehicleId, supposedly a variable holding the user's input, is matched.
There are many solutions for this problem. I'll not give you a full solution but will try to guide you.
One solution would be iterating on the string char-by-char. The Character class contains many useful methods for this task.
Other solution would be using a regex and replaceAll non digits characters (\D) with the empty string. From here the way to the answer is very short.
Visit the String API and the regex tutorial to fuel your creative fire.