Iam trying to get the string october from the string ["october"]
i tried with replace method
tagNo = tagNo.replaceAll("\\[|\\]", ""); \\ it prints "october"
i cant replace quote with "".
tagNo = tagNo.replaceAll("\\[|\\]", "").replaceAll("\"", "\\\\\"");
not replacing quotes prints \"october\"
But i prefer to use a single code
using regex code how can i get the string with out quotes and square bracket
You need to slightly correct your regex. You haven't put any rule for removing double quote. Try using this,
tagNo = tagNo.replaceAll("(\\[|\"|\\])+", "");
tagNo = tagNo.replaceAll("[^a-zA-Z]+","");
tagNo = tagNo.replaceAll("\\[\"", "");
tagNo = tagNo.replaceAll("\"\\]", "");
Refer Java replace all square brackets in a string
Removing double quotes from a string in Java
Related
I have string like:
"-------5548481818fgh7hf8ghf----fgh54f4578"
I don't want to parse using Pattern and Matcher. I have code:
string.replaceAll("regex", ""));
How to make regex to exclude all symbols except a "-" to get string like:
-554848181878544578
You can use this negative lookahead regex:
String s = "-------5548481818fgh7hf8ghf----fgh54f4578";
String r = s.replaceAll("(?!^[-+])\\D+", "");
//=> -554848181878544578
(?!^-)\D will replace each non-digit except the hyphen at start.
RegEx Demo
This will work
String Str = new String("-------5548481818fgh7hf8ghf----fgh54f4578-");
String tmp = Str.replaceAll("([-+])+|([^\\d])","$1").replaceAll("\\d[+-](\\d|$)","");
System.out.println(tmp);
Ideone Demo
Alternative: Grab the opposite, instead of replacing the negative. Seems to be arbitrary that you've picked to remove characters you don't want, instead of grabbing the characters you do want. Example in javascript:
s = "-------5548481818fgh7hf8ghf----fgh54f4578"
s = '-' + s.match(/[0-9]+/g).join('')
// "-554848181878544578"
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]", ""));
I have string variable strVar with value as ' "value1" ' and i want to replace all the double quotes in the value with ' \" '. So after replacement value would look like ' \"value1\" '
How to do this in java?
Kindly help me.
You are looking for
str = str.replace("\"", "\\\"")
DEMO
I would avoid using replaceAll since it uses regex syntax in description of what to replace and how to replace, which means that \ will have to be escaped in string "\\" but also in regex \\ (needs to be written as "\\\\" string) which means that we would need to use
str = str.replaceAll("\"", "\\\\\"");
or probably little cleaner:
str = str.replaceAll("\"", Matcher.quoteReplacement("\\\""))
With replace we have escaping mechanism added automatically.
actually it is:
strVar.replaceAll("\"", "\\\\\"");
For example take a string which has structure like this--->>>
String obj = "hello"How are"you";
And you want replace all double quote with blank value or in other word,if you want to trim all double quote.
Just do like this,
String new_obj= obj.replaceAll("\"", "");
Strings are formatted with double quotes. What you have is single quotes, used for chars. What you want is this:
String foo = " \"bar\" ";
This should give you what you want;
System.out.println("'\\\" value1 \\\"'");
To replace double quotes
str=change condition to"or"
str=str.replace("\"", "\\"");
After Replace:change condition to\"or\"
To replace single quotes
str=change condition to'or'
str=str.replace("\'", "\\'");
After Replace:change condition to\'or\'
String s ="SSR/DANGEROUS GOODS AS PER ATTACHED SHIPPERS
/DECLARATION 1 PACKAGE
NFY
/ACME CONSOLIDATORS"
How to strip the space between "PACKAGE" and "NFY" ?
Java's String.replaceAll in fact takes a regular expression. You could remove all newlines with:
s = s.replaceAll("\\n", "");
s = s.replaceAll("\\r", "");
But this will remove all newlines.
Note the double \'s: so that the string that is passed to the regular expression parser is \n.
You can also do this, which is smarter:
s = s.replaceAll("\\s{2,}", " ");
This would remove all sequences of 2 or more whitespaces, replacing them with a single space. Since newlines are also whitespaces, it should do the trick for you.
Try this code:
s = s.replaceAll( "PACKAGE\\s*NFY", "PACKAGE NFY" );
s = s.replaceAll("[\\n\\r]", "");
Have you tried a replace function? Something in the lines of:
youString.Replace("\r", "")
string = string.replace(/\s{2,}/g, ' ');
I have a String called persons.name
I want to replace the DOT . with /*/ i.e my output will be persons/*/name
I tried this code:
String a="\\*\\";
str=xpath.replaceAll("\\.", a);
I am getting StringIndexOutOfBoundsException.
How do I replace the dot?
You need two backslashes before the dot, one to escape the slash so it gets through, and the other to escape the dot so it becomes literal. Forward slashes and asterisk are treated literal.
str=xpath.replaceAll("\\.", "/*/"); //replaces a literal . with /*/
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceAll(java.lang.String,%20java.lang.String)
If you want to replace a simple string and you don't need the abilities of regular expressions, you can just use replace, not replaceAll.
replace replaces each matching substring but does not interpret its argument as a regular expression.
str = xpath.replace(".", "/*/");
Use Apache Commons Lang:
String a= "\\*\\";
str = StringUtils.replace(xpath, ".", a);
or with standalone JDK:
String a = "\\*\\"; // or: String a = "/*/";
String replacement = Matcher.quoteReplacement(a);
String searchString = Pattern.quote(".");
String str = xpath.replaceAll(searchString, replacement);
return sentence.replaceAll("\s",".");