Using Java, I want to go through the lines of a text and replace all "" symbols with a null entity reference.
Here is a sample string
String str = "asdsadas:\"\":asdasdASD:\"\":aSdasdcsC";
Result wanted is
String resStr = "asdsadas:null:asdasdASD:null:aSdasdcsC"
First, your string is not correct. In java you need to scape QUOTES:
String str = "asdsadas:\"\":asdasdASD:\"\":aSdasdcsC";
After, you need to escape quotes also to replace them:
String resStr= str.replaceAll("\"\"","null");
System.out.println(resStr);
OUTPUT
asdsadas:null:asdasdASD:null:aSdasdcsC
OR if you need QUOTES in the output:
String str = "\"asdsadas:\"\":asdasdASD:\"\":aSdasdcsC\"";
String resStr= str.replaceAll("\"\"","null");
System.out.println(resStr);
OUTPUT
"asdsadas:null:asdasdASD:null:aSdasdcsC"
Related
How can I use split function in java using two delimiters in the same string
I want to get the words with commas and spaces separately
String I = "hello,hi hellow,bye"
I want to get the above string splited as
String var1 = hello,bye
String var2 = hi hellow
Any suggestion is very much valued.
I would try to first split them with one of the delimiters, then for each resulting substring split with the other delimiter.
I am storing below number as a string. How can I remove the first three characters & last one character from the string?
String No = "00098000002208";
The easiest and probably fastest way would be to just use the base string function substring:
String input = "00034000004409";
String output = input.substring(3, input.length()-1);
We could also try doing a regex replacement:
String output = input.replaceAll("^.{3}(.*).$", "$1");
I have a String as shown below:
String s = "A, Category, \"Agriculture, forestry and fishing\",";
I want to remove spaces around comma (which are outside quotes). So my string should look like:
A,Category,"Agriculture, forestry and fishing",
I tried following RE:
String s = s.replaceAll("[,]\\s+", ",");
but output is:
A,Category,"Agriculture,forestry and fishing",
What changes should I do in my regular expression to avoid changes for commas inside quotes?
You can use this :
String str = "A, Category, \"Agriculture, forestry and fishing\",";
String result = str.replaceAll(" (?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", "");
//------------------------------^--------------------------------------
System.out.println(result);
This will print :
A,Category,"Agriculture, forestry and fishing",
//----------------------^---------------------
The space inside the quotes is not changes, just outside the quotes.
Here is a code DEMO and here is a regex DEMO
EDIT
This part is from #Exception_al so he suggest to use this :
String result = str.replaceAll("(\\s*,\\s*)(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", ",");
This solution is very good if you want to replace all spaces between comma , and the word.
You can check how this can work in regex DEMO
Java- Extract part of a string between two similar special characters.
I want to substring the second number, example :
String str = '1-10-251';
I want the result to be: 10
String str = "1-10-251";
String[] strArray = str.split("-");
System.out.println(strArray[1]);
I have string like this String s="ram123",d="ram varma656887"
I want string like ram and ram varma so how to seperate string from combined string
I am trying using regex but it is not working
PersonName.setText(cursor.getString(cursor.getColumnIndex(cursor
.getColumnName(1))).replaceAll("[^0-9]+"));
The correct RegEx for selecting all numbers would be just [0-9], you can skip the +, since you use replaceAll.
However, your usage of replaceAll is wrong, it's defined as follows: replaceAll(String regex, String replacement). The correct code in your example would be: replaceAll("[0-9]", "").
You can use the following regex: \d for representing numbers. In the regex that you use, you have a ^ which will check for any characters other than the charset 0-9
String s="ram123";
System.out.println(s);
/* You don't need the + because you are using the replaceAll method */
s = s.replaceAll("\\d", ""); // or you can also use [0-9]
System.out.println(s);
To remove the numbers, following code will do the trick.
stringname.replaceAll("[0-9]","");
Please do as follows
String name = "ram varma656887";
name = name.replaceAll("[0-9]","");
System.out.println(name);//ram varma
alternatively you can do as
String name = "ram varma656887";
name = name.replaceAll("\\d","");
System.out.println(name);//ram varma
also something like given will work for you
String given = "ram varma656887";
String[] arr = given.split("\\d");
String data = new String();
for(String x : arr){
data = data+x;
}
System.out.println(data);//ram varma
i think you missed the second argument of replace all. You need to put a empty string as argument 2 instead of actually leaving it empty.
try
replaceAll(<your regexp>,"")
you can use Java - String replaceAll() Method.
This method replaces each substring of this string that matches the given regular expression with the given replacement.
Here is the syntax of this method:
public String replaceAll(String regex, String replacement)
Here is the detail of parameters:
regex -- the regular expression to which this string is to be matched.
replacement -- the string which would replace found expression.
Return Value:
This method returns the resulting String.
for your question use this
String s = "ram123", d = "ram varma656887";
System.out.println("s" + s.replaceAll("[0-9]", ""));
System.out.println("d" + d.replaceAll("[0-9]", ""));