why is my string.length 0 if I split this string? [duplicate] - java

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 8 years ago.
This is probably a very easy question but I'm going to give you the code first.
import java.util.Scanner;
public class help {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Give: ");
String s = sc.next();
String[] parts = s.split(".");
System.out.println(parts.length);
}
}
Even if I give 192.168.1.1.1.1.1 or 1.2.3 or ... the parts.length will always be 0, can somebody please explain to me why and how I can let it be 4 if i enter 1.2.3.4?

You need s.split("\\.") because the argument to split is a regular expression. The . character in a regular expression means "any character", and you need to escape it with the backslash to have it mean "dot".

Because "." is a special character, meaning "any character".
You need to escape it to be able to use it as the character ".":
String[] parts = s.split("\\.");

Related

How to print the newline character instead of actually going to newline [duplicate]

This question already has answers here:
Find and replace all NewLine or BreakLine characters with \n in a String - Platform independent
(3 answers)
Closed 2 years ago.
I have the following code, which parses and replace whenever it finds a pattern in the string.
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class HelloWorld {
public static void main(String []args){
String str = "nv13=4t543r523w4r2w3e4r\n8nv13=4t543r523w4r2w3e4r\n8nv13=4t543r523w4r2w3e4r\n8nv13=4t543r523w4r2w3e4r\n8nv13=4t543r523w4r2w3e4r\n8nv13=4t543r523w4r2w3e4r\n5";
String newstr = str.replaceAll("(?<=nv13=)(.*\n?)(?=\n8)", "hello");
System.out.println(newstr);
}
}
I expect an output of
nv13=hello\n8nv13=hello\8nv13=hello\n8nv13=hello8nv13=hello\n8nv13=4t543r523w4r2w3e4r\n5
but,instead, I get
nv13=hello
8nv13=hello
8nv13=hello
8nv13=hello
8nv13=hello
8nv13=4t543r523w4r2w3e4r
5
What am I missing the above code to fix the \n , to actually print it out in the string without going to the next line ?
newstr = newstr.replace("\n", "\\n");
You can escape the \ like this \\. So this will print \n and not actually go on a new line.
Output:
nv13=hello\n8nv13=hello\n8nv13=hello\n8nv13=hello\n8nv13=hello\n8nv13=4t543r523w4r2w3e4r\n5

How to remove dots from a string in Java? [duplicate]

This question already has answers here:
How to replace a String in java which contains dot?
(3 answers)
Closed 3 years ago.
In my program I want to remove all the dots(.) I've tried to do this but it's not working.
public class MyClass {
public static void main(String args[]) {
String str = " .Hello.World..Awesome!. ";
System.out.println(str.replaceAll(".",""));
}
}
Replace all takes a regex as the first argument and you need to escape "." like
"\\."
str.replaceAll("\\.","")
This is not working because . is a special regex character. You must escape this using a backslash. The dot is used as a capture all in regex.
System.out.println(str.replaceAll("\\.",""));
You can read up on all of the special characters in regex here - https://www.regular-expressions.info/characters.html

Regex for split method in String class which split the text by the condition [duplicate]

This question already has answers here:
Regex to match only commas not in parentheses?
(3 answers)
Closed 3 years ago.
I've the string looking like this:
word1-word2-word3-\d{1,2}\w?-\d{1,2}\w?|word1-word2-\d{1,2}\w?-\d{1,2}\w?|word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w
I'd like to split this string by '|' everywhere where it doesn't precedes by '('. So result should be:
["word1-word2-word3-\d{1,2}\w?-\d{1,2}\w?", "word1-word2-\d{1,2}\w?-\d{1,2}\w?", "word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w"]
I've trying to use negative lookahead \((?!\|) which split the text to on '('.
UPDATE
So I want to achieve not splitting the "word1-word2-word3-(\d{1,2}\w?\d{1,2}|\d{1,2}\w?)-\d{1,2}\w" on '|' where that character is precedes by '('.
Could someone please help me with this?
Code:
public static void main(String[] args) {
String str = "word1-word2-word3-\\d{1,2}\\w?-\\d{1,2}\\w?|word1-word2-\\d{1,2}\\w?-\\d{1,2}\\w?|word1-word2-word3-(\\d{1,2}\\w?\\d{1,2}|\\d{1,2}\\w?)-\\d{1,2}\\w";
String[] arrOfStr = str.split("\\|");
for (String a : arrOfStr)
System.out.println(a);
}

Regex for Delimeter [duplicate]

This question already has answers here:
Is it possible to match nested brackets with a regex without using recursion or balancing groups?
(2 answers)
Closed 5 years ago.
I am trying to write a regex for delimiters “(“, “)”, “,”. I tried to write a regex but it is not the correct for the delimeters.
Let's say the input is mult(3,add(2,subs(4,3))). The output with my delimeter regex is: 3,add(2,subs(4,3.
public class Practice {
private static final String DELIMETER = "\\((.*?)\\)";
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String arg = reader.next();
Pattern p = Pattern.compile(DELIMETER);
Matcher m = p.matcher(arg);
while (m.find()) {
System.out.println(m.group(1));
}
}
}
What is the correct regex to get string between the delimeters?
In general, you cannot use a regex to match anything which can nest recursively. However, if you removed the ? from your regex, it would match from the first ( to the last ), which might be good enough, depending on what you expect the input to look like.

remove all special characters in java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Replacing all non-alphanumeric characters with empty strings
import java.util.Scanner;
import java.util.regex.*;
public class io{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
String c;
if((c=scan.nextLine())!=null)
{
Pattern pt = Pattern.compile("[^a-zA-Z0-9]");
Matcher match= pt.matcher(c);
while(match.find()){
c=c.replace(Character.toString(c.charAt(match.start())),"");
}
System.out.println(c);
}
}
}
Case 1
Input : hjdg$h&jk8^i0ssh6
Expect : hjdghjk8i0ssh6
Output : hjdgh&jk8^issh6
Case 2
Input : hjdgh&jk8i0ssh6
Expect : hjdghjk8i0ssh6
Output : hjdghjk8i0ssh6
Case 3
Input : hjdgh&j&k8i0ssh6
Expect : hjdghjk8i0ssh6
Output : hjdghjki0ssh6
Anyone please help me to figure out, what is wrong in my code logic ??
use [\\W+] or "[^a-zA-Z0-9]" as regex to match any special characters and also use String.replaceAll(regex, String) to replace the spl charecter with an empty string. remember as the first arg of String.replaceAll is a regex you have to escape it with a backslash to treat em as a literal charcter.
String c= "hjdg$h&jk8^i0ssh6";
Pattern pt = Pattern.compile("[^a-zA-Z0-9]");
Matcher match= pt.matcher(c);
while(match.find())
{
String s= match.group();
c=c.replaceAll("\\"+s, "");
}
System.out.println(c);
You can read the lines and replace all special characters safely this way.
Keep in mind that if you use \\W you will not replace underscores.
Scanner scan = new Scanner(System.in);
while(scan.hasNextLine()){
System.out.println(scan.nextLine().replaceAll("[^a-zA-Z0-9]", ""));
}
Your problem is that the indices returned by match.start() correspond to the position of the character as it appeared in the original string when you matched it; however, as you rewrite the string c every time, these indices become incorrect.
The best approach to solve this is to use replaceAll, for example:
System.out.println(c.replaceAll("[^a-zA-Z0-9]", ""));

Categories

Resources