Find String 2017 from a given string="Date-01-2017" [duplicate] - java

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 5 years ago.
I have the code:
String str="Date-01-2017"
How can I print 2017 only from this string?
I don't know how to split and get the return as 2017.

You can try:
String[] splitString = dateString.split("-");
String year = splitString[2];

You can use substring method to take the part of the string you need:
String data = "Date-01-2017";
String year = data.substring(data.length() - 4, data.length());

You could use regular expression:
Pattern pattern = Pattern.compile("\\w+-(?<month>\\d{2})-(?<year>\\d{4})");
Matcher matcher = pattern.matcher("Date-01-2017");
if(matcher.matches())
System.out.println(matcher.group("year"));
else
System.out.println("NOT MATCH!!!");
demo

Another way to print if the string "2017" is using a function, such as this.
public boolean contains(CharSequence s) {
return indexOf(s.toString()) > -1;
}
You can make a call to this function, with the string as the parameter, and if the string contains 2017, then u can print out that string.

Related

How to remove a substring from a string without using replace() method? [duplicate]

This question already has answers here:
How can I remove a substring from a given String?
(14 answers)
Closed 2 years ago.
String string = "yesnoyesnoyesnoyesno";
String substring = "no";
How do I remove every occurrence of the substring from the string so it ends up being "yesyesyesyes"?
If you are using Java 8+, then you could try splitting the input on "no" and then joining the resulting array together into a string:
String string = "yesnoyesnoyesnoyesno";
string = String.join("", string.split("no"));
System.out.println(string);
This prints:
yesyesyesyes
This doesn't use the replace(), so technically it meets the brief:
String str = string.replaceAll("no", "");
String input = "yesnoyesnoyesno";
String output="";
for(String token : input.split("no") )
output = output.concat(token);
System.out.println(output);
It prints:
yesyesyesyes

Java String remove all characters after number [duplicate]

This question already has answers here:
Java: removing numeric values from string
(7 answers)
Closed 5 years ago.
This is my string address : London, Jon 2 A
And I want to see in my output see London, Jon
I tried to do this :
String result = chapterNumber.substring(0, chapterNumber.indexOf("1"));
But I have to do 10 times from different number maybe is better way to do this
try this
String str = "London, Jon 2 A";
System.out.println(str.replaceAll("\\d.*",""));
You can use regex with groups to get only the first group (the sequence of non-digit chars until the first digit):
String result = chapterNumber.replaceAll("([^\\d])(\\d.*)", "$1");
String s = "London, Jon 2 A";
Matcher matcher = Pattern.compile("\\d+").matcher(s);
matcher.find();
int i = Integer.valueOf(matcher.group());
System.out.println(s.substring(0,s.indexOf(String.valueOf(i))));
String result = chapterNumber.substring(0, chapterNumber.indexOf(","));

String splitting [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
Say I have a string a such:
String str = "Kellogs Conflakes_$1.20";
How do I get the preceding values before the dollar ($) sign.
N.B: The prices could be varied say $1200.
You can return the substring using substring and the index of the $ character.
str = str.substring(0, str.indexOf('$'));
You could use String.split(String s) which creates a String[].
String str = "Kellogs Conflakes_$1.20"; //Kellogs Conflakes_$1.20
String beforeDollarSign = String.split("$").get(0); //Kellogs Conflakes_
This will split the String str into a String[], and then gets the first element of that array.
Just do this for split str.split("$") and store it on an array of String.
String[] split = str.split("$");
And then get the first position of the array to get the values that you have before the $
System.out.println(split[0]); //Kellogs Conflakes_
At the position 1 you will have the rest of the line:
System.out.println(split[1]); //1.20
Try this:
public static void main(String[] args)
{
String str = "Kellogs Conflakes_$1.20";
String[] abc=str.split("\ \$");
for(String i:abc)
{
System.out.println(i);
}
}
after this you can easily get abc[0]

How to grab piece of a string from a bigger string using regex or any method [duplicate]

This question already has answers here:
Using Java to find substring of a bigger string using Regular Expression
(11 answers)
Closed 7 years ago.
I'm trying to extract a piece of string from a larger string.
Example:
String value;
...etc...
value = someMethod();
// Here value equals a large string text
value;
I want to extract a subset of this string which begins with "path=" and everything after it.
Elaborated Example:
if value equals:
StartTopic topic=testParser, multiCopy=false, required=true,
all=false, path=/Return/ReturnData/IRSW2
I want only "path=/Return/ReturnData/IRSW2" so on and so forth.
How could I do this in Java?
This is what I currently have:
if(value.contains("path")) {
String regexStr = FileUtils.readFileToString(new File("regex.txt"));
String escapedRegex = StringEscapeUtils.escapeJava(regexStr);
System.out.println(value.replaceAll(escapedRegex), "$1");
}
This doesn't work! Just outputs the whole string again
Contents of regex.txt:
/path=([^\,]+)/
This should do the trick
String s = "if value=StartTopic topic=testParser, multiCopy=false, required=true, all=false, path=/Return/ReturnData/IRSW2";
String regex= "path=[^\\,]*";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(s);
if(m.find()) {
System.out.println(m.group());
}
You can also use:
String regex = "(?<=path=)[^\\,]*";
insted, so you will get only /Return/ReturnData/IRSW2 part.
Use the function indexOf() to find the index of 'path='
String str = "path=/Return/ReturnData/IRSW2";
System.out.println(str.substring(str.indexOf("path=") + 5));

empty array, split method [duplicate]

This question already has answers here:
The split() method in Java does not work on a dot (.) [duplicate]
(7 answers)
Closed 9 years ago.
My problem is that the array ms[ ] doesn't get values when I do split( );
Why is this happening ?
public class Test {
public static void main(String[] args) {
Date date = new Date();
SimpleDateFormat ft = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss."); //change format
String msgTime = ft.format(date);
System.out.println(msgTime);
String ms[] = msgTime.split(".");
System.out.println(ms.length);
}
}
The problem is split() function takes regular expression as argument, not a simple string. And "." regular expression means "any symbol".
So you need just escape it.
String ms[] = msgTime.split("\\.");
I'm guessing you meant to do
String ms[] = msgTime.split("\\.");
String.split() takes a regular expression so you should escape any special characters, such as ..

Categories

Resources