char[] charArray = {'\n'};
String str = String.valueOf(charArray);
System.out.println(str);
So I want to obtain a string of the newline character that is in the array.
However, with this code, the output is simply a blank space. How can I get
String str= "\n" ?
'\n' is the encoding for a newline character. Perhaps you want:
String str = "\\n"
or perhaps you want
char[] charArray = {'\\','n'};
String str = String.valueOf(charArray)
Related
I am trying to reverse the characters in a string separated by a delimiter I provide.
Input: string: "Abc.134dsq" , delimiter: "."
Desired Output: cbA.qsd431
My attempt:
String fileContent = "Abc.134dsq";
String delimiter = ".";
fileContent = fileContent.replace(delimiter, "-");
String[] splitWords = fileContent.split("-");
StringBuilder stringBuilder = new StringBuilder();
for (String word : splitWords) {
StringBuilder output = new StringBuilder(word).reverse();
stringBuilder.append(output);
}
System.out.println(stringBuilder.toString());
Try this:
System.out.println(Arrays
.stream("Abc.134dsq".split("\\.", -1))
.map(StringBuilder::new)
.map(StringBuilder::reverse)
.collect(Collectors.joining(".")));
See live demo.
This handles the “preserving the trailing dot” scenarios mentioned in the comments. Live demo shows this aspect too.
Enough time has passed that your homework deadline has passed, so I thought I’d show you this one-liner.
I have to display string with visible control characters like \n, \t etc.
I have tried quotations like here, also I have tried to do something like
Pattern pattern = Pattern.compile("\\p{Cntrl}");
Matcher matcher = pattern.matcher(str);
String controlChar = matcher.group();
String replace = "\\" + controlChar;
result = result.replace(controlChar, replace);
but I have failed
Alternative: Use visible characters instead of escape sequences.
To make control characters "visible", use the characters from the Unicode Control Pictures Block, i.e. map \u0000-\u001F to \u2400-\u241F, and \u007F to \u2421.
Note that this requires output to be Unicode, e.g. UTF-8, not a single-byte code page like ISO-8859-1.
private static String showControlChars(String input) {
StringBuffer buf = new StringBuffer();
Matcher m = Pattern.compile("[\u0000-\u001F\u007F]").matcher(input);
while (m.find()) {
char c = m.group().charAt(0);
m.appendReplacement(buf, Character.toString(c == '\u007F' ? '\u2421' : (char) (c + 0x2400)));
if (c == '\n') // Let's preserve newlines
buf.append(System.lineSeparator());
}
return m.appendTail(buf).toString();
}
Output using method above as input text:
␉private static String showControlChars(String input) {␍␊
␉␉StringBuffer buf = new StringBuffer();␍␊
␉␉Matcher m = Pattern.compile("[\u0000-\u001F\u007F]").matcher(input);␍␊
␉␉while (m.find()) {␍␊
␉␉␉char c = m.group().charAt(0);␍␊
␉␉␉m.appendReplacement(buf, Character.toString(c == '\u007F' ? '\u2421' : (char) (c + 0x2400)));␍␊
␉␉␉if (c == '\n')␍␊
␉␉␉␉buf.append(System.lineSeparator());␍␊
␉␉}␍␊
␉␉return m.appendTail(buf).toString();␍␊
␉}␍␊
Simply replace occurences of '\n' with the escaped version (i.e. '\\n'), like this:
final String result = str.replace("\n", "\\n");
For example:
public static void main(final String args[]) {
final String str = "line1\nline2";
System.out.println(str);
final String result = str.replace("\n", "\\n");
System.out.println(result);
}
Will yield the output:
line1
newline
line1\nnewline
just doing
result = result.replace("\\", "\\\\");
will work!!
There are several answers to similar questions as mine, but I have tried several of them and they are not working. I must be doing something stupid.
I have
String newline = System.getProperty("line.separator");
String content = "Test\n another line\n";
if(content.contains("\\n")) {
content = content.replaceAll("(\\n)", newline);
System.out.print(content);
}
I also tried "\n" and "\\n" in the regex. The content remains unchanged using replaceAll.
Okay facts:
\r is a CR, U+000D
\n is a LF, U+000A
Those characters you can put in a String
String s = "line 1.\nline 2.\n";
String newline = System.getProperty("line.separator");
newline can be "\n" (1 char) or "\r\n" (2 chars) or still something else.
If you would read this text, reading first a backslash and then an n, it would be in code:
String nl = "\\n"; // Two chars, an escaped backslash and a `n`.
String nl = "\\" + 'n'; // Two chars, an escaped backslash and a `n`.
If you would want to replace these two chars with a real newline:
s = s.replace("\\n", "\n");
s = s.replace("\\n", newline); // Platform dependent
Now java regex is still more complex, as it escapes regex letters with a backslash, which in Strings is escaped itself:
You will not need a regex replaceAll/replaceFirst here, but it would go as:
s = s.replaceAll("\\\\n", "\n");
The pattern containing two backslashes: regex escaping of one backslash.
String newline = System.getProperty("line.separator");
String content = "Test\\n another line\\n";
if(content.contains("\\n")) {
content = content.replaceAll("\\\\n", newline);
System.out.print(content);
}
The extra 2 slashes are the escape characters
I also tried this and it works
content.replace("\\n", "\\r\\n")
but
content.replaceAll("\\n", "\\r\\n")
does not.
So in the end I used
while(content.contains("\\n")) {
content = content.replace("\\n", newline);
}
And this solves my problem, not elegant, but it works.
Here is my code where i read a txt file and create 2 string from its content now i need to create 2 char array with these 2 strings. How can i do this?
java.io.File file=new java.io.File("deneme3.txt");
try{
Scanner input=new Scanner(file);
while(input.hasNext()){
String num= input.nextLine();
String[] parts =num.split(" ");
String part1=parts[0];
String part2=parts[1];
in the end i need to have something like;
char[] mSeqA and char[] mSeqB;
char[] mSeqA=parts[0].toCharArray();
char[] mSeqB=parts[1].toCharArray();
To convert a string to a char array you can simply write
String str = "someString";
char[] charArray = str.toCharArray();
So in your case it would be
char[] mSeqA = parts[0].toCharArray();
char[] mSeqB = parts[1].toCharArray();
See http://docs.oracle.com/javase/7/docs/api/java/lang/String.html for details
I need to split string with delimiter of \n when I use this code:
String delimiter = "\n";
String[] temp;
temp = description2[position].split(delimiter);
for (int i = 0; i < temp.length; i++) {
holder.weeklyparty_text3.setSingleLine(false);
holder.weeklyparty_text3.setText(temp[i]);
}
but not get split string from \n.
You need to escape the backslash in the delimiter string: "\\n"
Split uses regex - so to split on a newline, you should use:
String delimiter = "\\n";
In order to support Unix and Windows new lines use:
String lines[] = String.split("\r?\n");
as described in:
Split Java String by New Line