Dealing with different String date formats - java

In java, I have a string with a date in dd-mm-yyyy format:
String value = "31-01-1989";
Now, I want the value in another variable to be ddmmyyyy format:
String value = "31011989";
How to do this?

In this case you can simply remove dashes
value = value.replace("-", "");
or
value = value.replaceAll("-", "");
but according to my tests the first version is a little bit faster. So I personally prefer to use replaceAll only when the first parameter is a regex.
Note that, despite a confusion in the names, String.replace replaces ALL substrings that match the first arg, just as String.replaceAll does. The main difference is that the String.replace treats the first arg as a string literal and String.replaceAll uses it as a regex.

easy solution:
String value1 = "31-01-1989";
String value2 = value1.replace("-", "");

Have a look at SimpleDateFormat for a general solution. Write a SimpleDateFormat to parse the first date and use format in another to have the expected output.

You can use String.replace(Charseq, Charseq) to remove the delimiters.
String value = "31-01-1989";
String value2 = value.replace("-", "");
System.out.println(value2);

Related

Java replaceAll - regex variable not replaced

I have one variable representing the regex, when run replaceAll, none of string is replaced. Please help to take a look.
String s = "Issue 3 for 5 describe the title";
String regex = "Issue\\s\\d+\\sfor\\s\\d+";
System.out.println(s.replaceAll(regex, "test"));
replaceAll returns the modified String, but it does not modify the original String, as String in Java is immutable.
You need to:
String resultString = s.replaceAll(regex, "test")
System.out.println(resultString);
Java String is immutable.
If you want to change string s use this:
s = s.replaceAll(regex, "test"));
This could be caused by the fact that in your code you have to double escape your regexp, but in the xml you don't:
String regex = "Issue\\s\\d+\\sfor\\s\\d+";
is equivalent to the parsed
<regex>Issue\s\d+\sfor\s\d+</regex>

Efficient way to perform various String operations

I have a Script in which i have to extract string from strings, get the last index, get nth character in a string, comparisons, string contains a string or not etc etc. I would like to know the best method/ practice to do such operations on strings in java. Should i use StringBuilder all the time to perform the above operations. In few cases i have used regular expressions to find strings.
So what should i use?
Scenario is : loads of comparisons and indexes have to found. Thanks.!
example :
String name = "application/octet-stream; name=\"2012-04-20.tar.gz\""; // get 2012-04-20.tar.gz
Pattern pattern = Pattern.compile("\"(.*?)\"");
Matcher matcher = pattern.matcher(name);
if (matcher.find()) {
System.out.println(matcher.group(1));
}
String date = " Number 4, December 2013";
String year = date.substring(date.lastIndexOf(" ")+1);
String month = date.substring(date.indexOf(',')+2,date.indexOf(" ",date.indexOf(',')+2 ));
date = year+getMonth(month)+"01";
System.out.println(date);
Like above, many other extraction of string within string.
When you deal with large amount of String object use String intern function wisely to conserve heap space and eliminate object creation overhead. refer here for more information
Since you are not manipulating the strings, you can use String or StringUtils instead of StringBuilder
All of these operations can be done with the String class, see below for the methods you should use.
extract string from strings
String.substring()
get the last index
String.length() -1
the last character
s.charAt(s.length()-1);
get nth character in a string
String.charAt()
comparisons
String.compareTo()
string contains a string or not
String.contains()
You can also use String.toLowerCase() on both if necessary

Java Strings with Delimiters

I have a string "content/users/user/missions/mission" .I need to get "content/users/user/missions" from it [i.e. string upto the last delimiter] .How to proceed ?
If your requirement is that simple then you could do the following:
String string = "content/users/user/missions/mission";
String newString = string.substring(0, string.lastIndexOf('/'));
There are more fancy ways of doing this, regex could be one.
Use lastIndexOf and substring methods from String class.
String str = "content/users/user/missions/mission";
String result = str.substring(0,str.lastIndexOf('/'));

MessageFormat not formatting when single quotes are included

I am passing in text which is combination of {} filler and text. I am trying to fill {} with some values and tried using MessageFormat.
String sss = "{0}SomeText{1}\'.{2}SomeText{2}SomeText{0}{0}SomeText{2}{0}SomeText{0}{1}SomeText{0}{2}{0}{0}{1}{0}{2}{0}{0}{2}{0}{0}{1}{0}{2}{0}";
Object[] testArgs = {"nits1", "Nits2","nits#"};
System.out.println(MessageFormat.format(sss,testArgs));
OUTPUT
nits1SomeTextNits2.{2}SomeText{2}SomeText{0}{0}SomeText{2}{0}SomeText{0}{1}SomeText{0}{2}{0}{0}{1}{0}{2}{0}{0}{2}{0}{0}{1}{0}{2}{0}
The single quote must be escaped using a double single quote:
String sss = "{0}SomeText{1}''.{2}S..."
My mistake was, that I didn't use the returned value from method 'format(..)'
Wrong code:
MessageFormat.format(sss, testArgs);
System.out.println(sss);
Correct code:
String newString = MessageFormat.format(sss, testArgs);
System.out.println(newString);

Split the string

abcd+xyz
i want to split the string and get left and right components with respect to "+"
that is i need to get abcd and xyz seperatly.
I tried the below code.
String org = "abcd+xyz";
String splits[] = org.split("+");
But i am getting null value for splits[0] and splits[1]...
Please help..
The string you send as an argument to split() is interpreted as a regex (documentation for split(String regex)). You should add an escape character before the + sign:
String splits[] = org.split("\\+");
You might also find the Summary of regular-expression constructs worth reading :)
"+" is wild character for regular expression.
So just do
String splits[] = org.split("\\+");
This will work
the expression "+" means one or many in java regular expression.
split takes Regex as a argument hence the comparion given by you fails
So use
String org = "abcd+xyz";
String splits[] = org.split(""\+");
regards!!
Try:
String splits[] = org.split("\\+");

Categories

Resources