Isn't it possible to Change a specific digit/letter or even a space in a string and set it to another one?
Example:
String test = "name1 name2 name3 name4"
and I want to convert it into another String so that it could look like this:
String test2 = "name1+name2+name3+name4"
So how can I tell it to set all "spaces" to a +?
Use replaceAll()
test2=test.replaceAll("\\s","+");
Try String.replaceAll()
test2 = test.replaceAll("\\s","+");
Note :
(Regex) \s : (Description) A whitespace character, short for [ \t\n\x0b\r\f]
and since this is a special character it is preceeded by one more \
Use String#replaceAll("\\s", "+") method
You should look into repalceAll() in String class
test2=test1.replaceAll(" ","+");
What you'll need to do is search the string for the spaces and each time you've found one replace it with a designated character.
In Java there are functions to do this for you. Such as ReplaceAll(oldStr, newStr), where oldStr can accept a regular expression and newStr is the replacement.
String test = "name1 name2 name3 name4";
test = test.ReplaceAll(" ","+");
System.out.println(test);
The output is:
name1+name2+name3+name4
Instead of typing a space: " "
You may add the exact character code (ASCII code) you wish to replace by using:
Character.toString ((char) i)
Where i is the ASCII number.
So the equivalent would be:
test = test.ReplaceAll(Character.toString ((char) 32), Character.toString ((char) 43));
Hope this helps :)
Related
I have a string with \r\n, \r, \n or \" characters in it. How can I replace them faster?
What I already have is:
String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replace("\\r\\n", "\n").replace("\\r", "").replace("\\n", "").replace("\\", ""));
But my code does not look beautiful enough.
I found on the Internet something like:
replace("\\r\\n|\\r|\\n|\\", "")
I tried that, but it didn't work.
You can wrap it in a method, put /r/n, /n and /r in a list. iterate the list and replace all such characters and return the modified string.
public String replaceMultipleSubstrings(String original, List<String> mylist){
String tmp = original;
for(String str: mylist){
tmp = tmp.replace(str, "");
}
return tmp;
}
Test:
mylist.add("\\r");
mylist.add("\\r\\n");
mylist.add("\\n");
mylist.add("\\"); // add back slash
System.out.println("original:" + s);
String x = new Main().replaceMultipleSubstrings(s, mylist);
System.out.println("modified:" + x);
Output:
original:Kerner\r\n kyky\r hihi\n \"
modified:Kerner kyky hihi "
I don't know if your current replacement logic be correct, but it says now that either \n, \r, or \r\n gets replaced with empty string, and backslash also gets replaced with empty string. If so, then you can try the following regex replace all:
String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replaceAll("\\r|\\n|\\r\\n|\\\\", ""));
One problem I saw with your attempt is that you are calling replace(), not replaceAll(), so it would only do a single replacement and then stop.
String.replaceAll() can be used, in your question you tried to use String.replace() which does not interpret regular expressions, only plain replacement strings...
You also need to escape the \\ again, i.e. \\\\ instead of \\
String s = "Kerner\\r\\n kyky\\r hihi\\n \\\"";
System.out.println(s.replaceAll("\\\\r|\\\\n|\\\\\"", ""));
Output
Kerner kyky hihi
Note the differences between String.replaceAll() and String.replace()
String.replaceAll()
Replaces each substring of this string that matches the given regular
expression with the given replacement.
String.replace()
Replaces each substring of this string that matches the literal target
sequence with the specified literal replacement sequence.
Use a regular expression if you want to do all the replaces in one go.
http://www.javamex.com/tutorials/regular_expressions/search_replace.shtml
if i have for example String a = "8sin(30)+sin(40) + 3sin(30)"
String b = a;how I can replace only the first "sin" and the third for "*sin", mantaining the second "sin" the same?
in other word, how I can replace part of a string only in specific cases?
If you want to replace sin which have number before it you can use something like
yourString = yourString.replaceAll("(\\d+)sin","$1*sin");
replaceAll uses regular expression which represents
\\d+ string build from one or more digit characters (like 0, 12, 321...) - we will place this number in group 1
sin literal.
In replacement we are reusing match from group 1 via $1
Demo:
String a = "8sin(30)+sin(40) + 3sin(30)";
System.out.println(a.replaceAll("(\\d)+sin","$1*sin"));
Output: 8*sin(30)+sin(40) + 3*sin(30)
You can also use look-behind to check if before sin there is any digit
String a = "8sin(30)+sin(40) + 3sin(30)";
System.out.println(a.replaceAll("(?<=\\d)sin","*sin"));
You are probably looking for the replaceFirst(String regex, String replacement) method of String.
You can use below regex:
[0-9]+sin
Demo
b=b.replaceFirst("[0-9]+sin","*sin");
Also you can use the substring(int beginIndex) method.
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]", ""));
For example: after execution, the output of the String "hello world yo" and "hello world yo" should be strictly the same.
what's more, the output should be a String[] in which:
String[0] == "hello"; String[1] == "world"; String[2] == "yo";
so that other method can deal with the effective words latter.
I was thinking about String.split(" "), but the blanks between the words are uncertain, and will then cause an exception..
You can use
String.split("\\s+") // one or more whitespace.
Dont use == for string comaprision instead use String.equals()
Edit for question in comment
what's the notation called? what if there is one or more "_" or "\n" ?
As you can see String#split() API accepts regex as parameter. The \s is shorthand character class for whitespace, whereas + is used to repeats the previous item once or more.
Now if you want to split String on
_ ie. underscore --> "this__is_test".split("[_]+");
\n ie. newline --> "this__is_test\n new line".split("\\r?\\n");
Regex Tutorial
You can split on "\\s+". That splits on one or more whitespace characters.
String.split() takes a regexp, so you can simply do String.split(" +").
I think the split function takes regex, but if it doesn't then the below works.
The regex in this might not be right, but it demonstrates the concept of what you're trying to do.
Pattern p = Pattern.compile("(.*?) *(.*)");
Matcher m = p.matcher(s);
if (m.matches()) {
String name = m.group(1);
String value = m.groupo(2);
}
for (int i = 1; i<=m.groupCount(); i++) {
System.out.println(m.group(i));
}
you can use Regular Expression to split the String.
String.split(Regular Expression);
for multiple whitespace, you can use Regular Expression: " \\s+ ", which 's' stand for space.
"==" operator used to judge whether left and right is equal. for String, they are Object actually, which means that they are regard as reference(like the pointer in C).
So if you want compare the content of two Strings, you can use method equals(String) of String.
e.g. str1.equals(str2)
I have string of the following form:
भन्ने [-0.4531954191090929, 0.7931147934270654, -0.3875088408737827, -0.09427394940704822, 0.10065554475134718, -0.22044284832864797, 0.3532556916833505, -1.8256229909222224, 0.8036832111904731, 0.3395868096795993]
Whereever [ or ] or , char are present , I just want to remove them and i want each of the word and float separated by a space. It is follows:
भन्ने -0.4531954191090929 0.7931147934270654 -0.3875088408737827 -0.09427394940704822 0.10065554475134718 -0.22044284832864797 0.3532556916833505 -1.8256229909222224 0.8036832111904731 0.3395868096795993
I am representing each of these string as line. i did following:
line.replaceAll("([|]|,)$", " ");
But it didn't work for me. There was nothing change in the input line. Any help is really appreciated.
Strings are immutable. Try
line = line.replaceAll("([|]|,)$", " ");
Or to be a bit more verbose, but avoiding regular expressions:
char subst = ' ';
line = line.replace('[', subst).replace(']', subst).replace(',', subst);
In Java, strings are immutable, meaning that the contents of a string never change. So, calling
line.replaceAll("([|]|,)$", " ");
won't change the contents of line, but will return a new string. You need to assign the result of the method call to a variable. For instance, if you don't care about the original line, you can write
line = line.replaceAll("([|]|,)$", " ");
to get the effect you originally expected.
[ and ] are special characters in a regular expression. replaceAll is expected a regular expression as its first input, so you have to escape them.
String result = line.replaceAll("[\\[\\],]", " ");
Cannot tell what you were trying to do with your original regex, why you had the $ there etc, would need to understand what you were expecting the things you put there to do.
Try
line = "asdf [foo, bar, baz]".replaceAll("(\\[|\\]|,)", "");
The regex syntax uses [] to define groups like [a-z] so you have to mask them.