0

I am working on one project. Client and server should use \a\b as endings of all messages. So my message could be "text\a\b". This one should have 6 chars. I am getting errors while I wanted to use it because java doesn't know \a escape sequence. And a bigger problem is that I can´t detect this sequence. I have tried:

char a;
if(a == '\a'){
   break;
}

in a loop while I am reading input. But this doesn't work. I can't use the scanner because it blocks socket and timeout times up. Thanks a lot for your advice.

Luk
  • 3
  • 2
  • Welcome to Stack Overflow. What *exactly* do you mean by "Client and server should use \a\b as endings of all messages"? Which Unicode character do you expect to be represented by "\a", and which Unicode character do you expect to be represented by "\b"? – Jon Skeet Mar 22 '19 at 18:02
  • 1
    It would be helpful if you could post some more of your code. Right now it's a bit hard to tell what you're trying to do exactly. – Sebastiaan van den Broek Mar 22 '19 at 18:04
  • 1
    Use `\x07` or `\u0007` instead. – Wiktor Stribiżew Mar 22 '19 at 18:06
  • `\a` could be only string, because is not an escaped char. `\b` is an escaped char, but I think it is not what you want. `\b` represent backslash, which will delete the first char before `\b`. Here is a list of some [escaped chars](https://stackoverflow.com/questions/1367322/what-are-all-the-escape-characters) . – KunLun Mar 22 '19 at 18:09
  • 1
    @KunLun Backspace, not backslash. – David Conrad Mar 22 '19 at 18:52
  • @DavidConrad You are right. My mistake. – KunLun Mar 23 '19 at 12:20

2 Answers2

0

If you're looking for escape sequence for alarm/bell, then use octal escape sequence instead: '\007'

    String s = "text\007\b";
    assert s.charAt(4) == '\007';
    assert s.charAt(5) == '\b';
yachoor
  • 929
  • 1
  • 8
  • 18
0

Just in case you actually only need regex, take a look at this.

Assuming you're checking every character and I've understood your question:

boolean escapeFlag = false;
for (int i = 0; i < stringy.length(); i++) {
    if (escapeFlag) {
        if (stringy[i] == 'a') {
            ....
        }
    } else if (stringy[i] == '\') {
        escapeFlag == true;
    }
TheChubbyPanda
  • 1,721
  • 2
  • 16
  • 37