String split Java not working [duplicate] - java

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 8 years ago.
I have tried split with some other texts, its working fine there but not here. Can someone tell me what I did wrong here?
private static String fileName = "jjjj.txt";
private static String userName = "xxxx";
private static String password = "yyyy";
public static void main(String args[]){
String info = "UserName" +"|"+ userName + "|" + password + "|" + fileName;
String tempStr[] = info.split("|");
System.out.println(tempStr[0]);
System.out.println(tempStr[1]);
System.out.println(tempStr[2]);
System.out.println(tempStr[3]);
}
I am getting output as :
U
s
e
What should I do to get the output as:
UserName
xxxx
yyyy
jjjj.txt

You have to escape the | in your regular expression. This should work:
String tempStr[] = info.split("\\|");

Related

replaceAll Java method to remove "\\n" from String [duplicate]

This question already has answers here:
How to remove the backslash in string using regex in Java?
(3 answers)
Closed 4 years ago.
I have a simple treatement but I'm stuck
I have something like
"\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
public static void main(String[] args) {
String value = "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
String filtre1 = value.replaceAll("\"", "");
String filtre2 = filtre1.replaceAll("\\n", "");
System.out.println(filtre2);
}
the result is .. I have always "\n" I want to remove it
iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\neJzt3XecXVW99
Maybe:
String filtre2 = filtre1.replaceAll("\\n", "");
need to be :
String filtre2 = filtre1.replaceAll("\\\\n", "");
(sorry i cannot just add comment)
You could try this:
public static void main(String[] args) {
String value = "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
String filtre1 = value.replaceAll("\"", "");
String filtre2 = filtre1.replaceAll("\\\\n", "");
System.out.println(filtre2);
}
You basically have two choices, both of which are in the below code:
public static void main(String[] args) {
String value = "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
System.out.println(value.replace("\"", "").replace("\\n", ""));
System.out.println(value.replaceAll("\"|\\\\n", ""));
}

Replacing multiple substrings from a string in Java/Android [duplicate]

This question already has answers here:
Java Replacing multiple different substring in a string at once (or in the most efficient way)
(11 answers)
Closed 6 years ago.
Example:
String originalString = "This is just a string folks";
I want to remove(or replace them with "") : 1. "This is" , 2. "folks"
Desired output :
finalString = "just a string";
For a sole substring it's easy, we can just use replace/replaceAll.
I also know it works for various numeric values replace("[0-9]","")
But can that be done for characters?
You can create a function like below:
String replaceMultiple (String baseString, String ... replaceParts) {
for (String s : replaceParts) {
baseString = baseString.replaceAll(s, "");
}
return baseString;
}
And call it like:
String finalString = replaceMultiple("This is just a string folks", "This is", "folks");
You can pass multiple strings that are to be replaced after the first parameter.
It works like this:
String originalString = "This is just a string folks";
originalString = originalString.replace("This is", "");
originalString = originalString.replace("folks", "");
//originalString is now finalString

How to split String with "\" as a delimiter? and also how to replace "\" into ":"? [duplicate]

This question already has answers here:
java split function
(6 answers)
Closed 8 years ago.
Hi I would like to know what should I put in order to split the string into parts using "\" as a delimiter?
The String returned by the server will be as shown below
String test ="\\ipAddress\FolderA\InnerFolderA\abc.mp4"; << this one give me error ,thus i have to put in another "\",
String test ="\\ipAddress\\FolderA\\InnerFolderA\\abc.mp4";
String parts[] = test.split("\\");
String part0 = parts[0];
String part1 = parts[1];
String part2 = parts[2];
and when I run it, it gives me error:
Exception in thread "main" java.util.regex.PatternSyntaxException:
Unexpected internal error near index 1.
Also when I tried replacing the String using this line of code to replace the "\" as in
String output = test.replaceAll("\\", ":");
it also give me error .
Maybe you should use "\\" as the argument,not "\". Very glad to help you!
public static void main(String[] args) {
String test ="\\ipAddress\\FolderA\\InnerFolderA\\abc.mp4";
String parts[] = test.split("\\\\");
String part0 = parts[0];
String part1 = parts[1];
String part2 = parts[2];
System.out.println(part1);
}
public static void main(String[] args) {
String test = "\\ipAddress\\FolderA\\InnerFolderA\\abc.mp4";
String output = test.replaceAll("\\\\", ":");
System.out.println(output);
String parts[] = test.split("\\\\");
String part0 = parts[0];
String part1 = parts[1];
String part2 = parts[2];
System.out.println(part0);
System.out.println(part1);
System.out.println(part2);
}
output
:ipAddress:FolderA:InnerFolderA:abc.mp4
ipAddress
FolderA

Replace dash character in Java String [duplicate]

This question already has answers here:
Java String replace not working [duplicate]
(6 answers)
Closed 9 years ago.
I tried to replace "-" character in a Java String but is doesn't work :
str.replace("\u2014", "");
Could you help me ?
String is Immutable in Java. You have to reassign it to get the result back:
String str ="your string with dashesh";
str= str.replace("\u2014", "");
See the API for details.
this simply works..
String str = "String-with-dash-";
str=str.replace("-", "");
System.out.println(str);
output - Stringwithdash
It's quite easy. You can use an Apache library, that will be useful while you develop an application. This is apache-commons-lang. You can do the following:
public class Main {
public static void main(String[] args) {
String test = "Dash - string";
String withoutDash = StringUtils.replace(test, "-", "");
System.out.println(withoutDash);
}
}

Java how to replace backslash? [duplicate]

This question already has answers here:
String replace a Backslash
(8 answers)
Closed 6 years ago.
In java, I have a file path, like 'C:\A\B\C', I want it changed to ''C:/A/B/C'. how to replace the backslashes?
String text = "C:\\A\\B\\C";
String newString = text.replace("\\", "/");
System.out.println(newString);
Since you asked for a regular expression, you'll have to escape the '\' character several times:
String path = "c:\\A\\B\\C";
System.out.println(path.replaceAll("\\\\", "/"));
You can do this using the String.replace method:
public static void main(String[] args) {
String foo = "C:\\foo\\bar";
String newfoo = foo.replace("\\", "/");
System.out.println(newfoo);
}
String oldPath = "C:\\A\\B\\C";
String newPath = oldPath.replace('\\', '/');
To replace all occurrences of a given character :
String result = candidate.replace( '\\', '/' );
Regards,
Cyril

Categories

Resources