Replacing (not removing) special char in a String by other char
I need to replace "《br》"
to "<br>"
.
replaceAll
does not work, but if I do it in the variable window when debugging ... it works!
Same with indexOf("《")
, returns -1 but using the variable window returns 12
How can I replace it?
So the question is, how to make it work in the code
java android indexof replaceall
add a comment |
I need to replace "《br》"
to "<br>"
.
replaceAll
does not work, but if I do it in the variable window when debugging ... it works!
Same with indexOf("《")
, returns -1 but using the variable window returns 12
How can I replace it?
So the question is, how to make it work in the code
java android indexof replaceall
1
you probably dostring.replaceAll(...)
, but you should re-assign to a result:string = string.replaceAll(...)
– Alex Salauyou
Nov 14 '18 at 15:04
Try Evaluate with ASCII code this expression
– Daniel Carreto
Nov 14 '18 at 15:14
add a comment |
I need to replace "《br》"
to "<br>"
.
replaceAll
does not work, but if I do it in the variable window when debugging ... it works!
Same with indexOf("《")
, returns -1 but using the variable window returns 12
How can I replace it?
So the question is, how to make it work in the code
java android indexof replaceall
I need to replace "《br》"
to "<br>"
.
replaceAll
does not work, but if I do it in the variable window when debugging ... it works!
Same with indexOf("《")
, returns -1 but using the variable window returns 12
How can I replace it?
So the question is, how to make it work in the code
java android indexof replaceall
java android indexof replaceall
edited Nov 14 '18 at 15:06
Federico klez Culloca
16k134380
16k134380
asked Nov 14 '18 at 15:00
user2126958user2126958
265
265
1
you probably dostring.replaceAll(...)
, but you should re-assign to a result:string = string.replaceAll(...)
– Alex Salauyou
Nov 14 '18 at 15:04
Try Evaluate with ASCII code this expression
– Daniel Carreto
Nov 14 '18 at 15:14
add a comment |
1
you probably dostring.replaceAll(...)
, but you should re-assign to a result:string = string.replaceAll(...)
– Alex Salauyou
Nov 14 '18 at 15:04
Try Evaluate with ASCII code this expression
– Daniel Carreto
Nov 14 '18 at 15:14
1
1
you probably do
string.replaceAll(...)
, but you should re-assign to a result: string = string.replaceAll(...)
– Alex Salauyou
Nov 14 '18 at 15:04
you probably do
string.replaceAll(...)
, but you should re-assign to a result: string = string.replaceAll(...)
– Alex Salauyou
Nov 14 '18 at 15:04
Try Evaluate with ASCII code this expression
– Daniel Carreto
Nov 14 '18 at 15:14
Try Evaluate with ASCII code this expression
– Daniel Carreto
Nov 14 '18 at 15:14
add a comment |
2 Answers
2
active
oldest
votes
As String is immutable you are not changing original value of text
variable. replaceAll
creates new String with correct value and you have to assign it to variable to use it.
Reference of usage can be found here with some more explanation, but basic usage is:
String originalText ="some text with letters to replace";
String newTextWithReplacedValues = originalText.replaceAll("a","e");//replaces all occurrences of "a" to "e"
System.out.println(newTextWithReplacedValues);
You can see it working in the debugger since text.replaceAll(...)
returns proper value. It is just not changing original text
variable.
May be my question was not clear enough. I now that i must assigned the replacement to a string, But if you see the second image, it shows that pos1 == -1 , meaning it did not found the special char. I did this becouse when assign result of replaceAll to a new string, it did not do the replace. So I check if the special char was found with indexOf. I should explain better the problem.
– user2126958
Dec 9 '18 at 13:20
add a comment |
For me replacing just works fine.
public class Application
public static void main(String args)
String s = "《br》";
s = s.replace("《", "<").replace("》", ">");
System.out.println(s);
《 may not be a single character though since '《' gives an error but "《" works.
This is the Character documentation:
The set of characters from U+0000 to U+FFFF is sometimes referred to
as the Basic Multilingual Plane (BMP). Characters whose code points
are greater than U+FFFF are called supplementary characters. The Java
platform uses the UTF-16 representation in char arrays and in the
String and StringBuffer classes. In this representation, supplementary
characters are represented as a pair of char values, the first from
the high-surrogates range, (uD800-uDBFF), the second from the
low-surrogates range (uDC00-uDFFF).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303112%2freplacing-not-removing-special-char-in-a-string-by-other-char%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As String is immutable you are not changing original value of text
variable. replaceAll
creates new String with correct value and you have to assign it to variable to use it.
Reference of usage can be found here with some more explanation, but basic usage is:
String originalText ="some text with letters to replace";
String newTextWithReplacedValues = originalText.replaceAll("a","e");//replaces all occurrences of "a" to "e"
System.out.println(newTextWithReplacedValues);
You can see it working in the debugger since text.replaceAll(...)
returns proper value. It is just not changing original text
variable.
May be my question was not clear enough. I now that i must assigned the replacement to a string, But if you see the second image, it shows that pos1 == -1 , meaning it did not found the special char. I did this becouse when assign result of replaceAll to a new string, it did not do the replace. So I check if the special char was found with indexOf. I should explain better the problem.
– user2126958
Dec 9 '18 at 13:20
add a comment |
As String is immutable you are not changing original value of text
variable. replaceAll
creates new String with correct value and you have to assign it to variable to use it.
Reference of usage can be found here with some more explanation, but basic usage is:
String originalText ="some text with letters to replace";
String newTextWithReplacedValues = originalText.replaceAll("a","e");//replaces all occurrences of "a" to "e"
System.out.println(newTextWithReplacedValues);
You can see it working in the debugger since text.replaceAll(...)
returns proper value. It is just not changing original text
variable.
May be my question was not clear enough. I now that i must assigned the replacement to a string, But if you see the second image, it shows that pos1 == -1 , meaning it did not found the special char. I did this becouse when assign result of replaceAll to a new string, it did not do the replace. So I check if the special char was found with indexOf. I should explain better the problem.
– user2126958
Dec 9 '18 at 13:20
add a comment |
As String is immutable you are not changing original value of text
variable. replaceAll
creates new String with correct value and you have to assign it to variable to use it.
Reference of usage can be found here with some more explanation, but basic usage is:
String originalText ="some text with letters to replace";
String newTextWithReplacedValues = originalText.replaceAll("a","e");//replaces all occurrences of "a" to "e"
System.out.println(newTextWithReplacedValues);
You can see it working in the debugger since text.replaceAll(...)
returns proper value. It is just not changing original text
variable.
As String is immutable you are not changing original value of text
variable. replaceAll
creates new String with correct value and you have to assign it to variable to use it.
Reference of usage can be found here with some more explanation, but basic usage is:
String originalText ="some text with letters to replace";
String newTextWithReplacedValues = originalText.replaceAll("a","e");//replaces all occurrences of "a" to "e"
System.out.println(newTextWithReplacedValues);
You can see it working in the debugger since text.replaceAll(...)
returns proper value. It is just not changing original text
variable.
edited Nov 14 '18 at 15:23
answered Nov 14 '18 at 15:09
Tomasz BaworTomasz Bawor
757627
757627
May be my question was not clear enough. I now that i must assigned the replacement to a string, But if you see the second image, it shows that pos1 == -1 , meaning it did not found the special char. I did this becouse when assign result of replaceAll to a new string, it did not do the replace. So I check if the special char was found with indexOf. I should explain better the problem.
– user2126958
Dec 9 '18 at 13:20
add a comment |
May be my question was not clear enough. I now that i must assigned the replacement to a string, But if you see the second image, it shows that pos1 == -1 , meaning it did not found the special char. I did this becouse when assign result of replaceAll to a new string, it did not do the replace. So I check if the special char was found with indexOf. I should explain better the problem.
– user2126958
Dec 9 '18 at 13:20
May be my question was not clear enough. I now that i must assigned the replacement to a string, But if you see the second image, it shows that pos1 == -1 , meaning it did not found the special char. I did this becouse when assign result of replaceAll to a new string, it did not do the replace. So I check if the special char was found with indexOf. I should explain better the problem.
– user2126958
Dec 9 '18 at 13:20
May be my question was not clear enough. I now that i must assigned the replacement to a string, But if you see the second image, it shows that pos1 == -1 , meaning it did not found the special char. I did this becouse when assign result of replaceAll to a new string, it did not do the replace. So I check if the special char was found with indexOf. I should explain better the problem.
– user2126958
Dec 9 '18 at 13:20
add a comment |
For me replacing just works fine.
public class Application
public static void main(String args)
String s = "《br》";
s = s.replace("《", "<").replace("》", ">");
System.out.println(s);
《 may not be a single character though since '《' gives an error but "《" works.
This is the Character documentation:
The set of characters from U+0000 to U+FFFF is sometimes referred to
as the Basic Multilingual Plane (BMP). Characters whose code points
are greater than U+FFFF are called supplementary characters. The Java
platform uses the UTF-16 representation in char arrays and in the
String and StringBuffer classes. In this representation, supplementary
characters are represented as a pair of char values, the first from
the high-surrogates range, (uD800-uDBFF), the second from the
low-surrogates range (uDC00-uDFFF).
add a comment |
For me replacing just works fine.
public class Application
public static void main(String args)
String s = "《br》";
s = s.replace("《", "<").replace("》", ">");
System.out.println(s);
《 may not be a single character though since '《' gives an error but "《" works.
This is the Character documentation:
The set of characters from U+0000 to U+FFFF is sometimes referred to
as the Basic Multilingual Plane (BMP). Characters whose code points
are greater than U+FFFF are called supplementary characters. The Java
platform uses the UTF-16 representation in char arrays and in the
String and StringBuffer classes. In this representation, supplementary
characters are represented as a pair of char values, the first from
the high-surrogates range, (uD800-uDBFF), the second from the
low-surrogates range (uDC00-uDFFF).
add a comment |
For me replacing just works fine.
public class Application
public static void main(String args)
String s = "《br》";
s = s.replace("《", "<").replace("》", ">");
System.out.println(s);
《 may not be a single character though since '《' gives an error but "《" works.
This is the Character documentation:
The set of characters from U+0000 to U+FFFF is sometimes referred to
as the Basic Multilingual Plane (BMP). Characters whose code points
are greater than U+FFFF are called supplementary characters. The Java
platform uses the UTF-16 representation in char arrays and in the
String and StringBuffer classes. In this representation, supplementary
characters are represented as a pair of char values, the first from
the high-surrogates range, (uD800-uDBFF), the second from the
low-surrogates range (uDC00-uDFFF).
For me replacing just works fine.
public class Application
public static void main(String args)
String s = "《br》";
s = s.replace("《", "<").replace("》", ">");
System.out.println(s);
《 may not be a single character though since '《' gives an error but "《" works.
This is the Character documentation:
The set of characters from U+0000 to U+FFFF is sometimes referred to
as the Basic Multilingual Plane (BMP). Characters whose code points
are greater than U+FFFF are called supplementary characters. The Java
platform uses the UTF-16 representation in char arrays and in the
String and StringBuffer classes. In this representation, supplementary
characters are represented as a pair of char values, the first from
the high-surrogates range, (uD800-uDBFF), the second from the
low-surrogates range (uDC00-uDFFF).
answered Nov 14 '18 at 15:15
sezi80sezi80
112111
112111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53303112%2freplacing-not-removing-special-char-in-a-string-by-other-char%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
you probably do
string.replaceAll(...)
, but you should re-assign to a result:string = string.replaceAll(...)
– Alex Salauyou
Nov 14 '18 at 15:04
Try Evaluate with ASCII code this expression
– Daniel Carreto
Nov 14 '18 at 15:14