B4J Question [Solved]replacing a literal \n by something else

alwaysbusy

Expert
Licensed User
Longtime User
I've been searching a way to replace all literal \n occurrences by something else and it is driving me nuts all morning. note: It needs to be done in java (for a library), not B4J

B4X:
Private NativeMe As JavaObject
NativeMe = Me
Dim s As String = NativeMe.RunMethod("test", Array As String("this \n is a test"))
Log(s)

B4X:
public String test(String s) {
    return s.replaceAll("\\n", "ABA");
}

Result: this \n is a test

Expected: this ABA is a test

Same happens in pure java in my library.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub AppStart (Args() As String)
   Private NativeMe As JavaObject
   NativeMe = Me
   Dim s As String = NativeMe.RunMethod("test", Array As String("this \n is a test"))
   Log(s)
   
End Sub



#if java
public static String test(String s) {
    return s.replaceAll("\\\\n", "ABA");
}
#end if

Second option:
B4X:
#if java
public static String test(String s) {
    return s.replaceAll("\\Q\\n\\E", "ABA");
}
#end if
 
Upvote 0
Top