标准输入在不同的编程语言中有奇怪的字符

Standard Input having weird characters with them in different programming lanuage

我对这些编程语言的标准输入感到困惑:

[注:]

我添加了关于这么多编程语言的细节,因为在这个问题上它们的问题都是一样的,我在这个问题上唯一的 焦点 是如何克服这个问题和从程序本身中获得真正的终端体验。

首先,

Java

代码:

import java.util.Scanner;
public class Try{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a String : ");
    String s = sc.nextLine();
    System.out.println("The Entered String is : " + s);
    System.out.println("The Length of Entered String is : " + s.length());
    sc.close();
  }
}

输出:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $java Try
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $java Try
Enter a String : 
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8

当我按下箭头键时 ^[[C 出现而不是光标移动(类似的事情发生在其他箭头键、转义键、home、end 上)!

但即使在这种情况下,它是如何变成 8 的?当我打印它们时,为什么它们没有显示为“[”,'C',“^”是可打印的。

Python

此处与Java相同!

代码:

s = input("Enter a String : \n")
print("The Entered String is : " + s)
print("The Length of Entered String is : " + str(len(s)))

输出:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python try.py
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python try.py
Enter a String : 
hello^[[D
The Entered String is : hello
The Length of Entered String is : 8

C

这里也是一样..

代码:

#include <stdio.h>

int main()
{
   char s[20];
   int len = 0;
   printf("Enter a String : \n");
   scanf("%[^\n]%*c", s);
   while (s[len] != '[=14=]')
    len++;
   printf("The Entered String is : %s\n", s);
   printf("The Length of Entered String is : %d\n", len);
   return 0;
}

输出:

─[jaysmito@parrot]─[~/Desktop]
└──╼ $gcc -o tryc -Os try.c
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String : 
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8

但代码略有不同:

代码:

#include <stdio.h>
#define MAX_LIMIT 20
int main()
{
   char s[MAX_LIMIT];
   int len = 0;
   printf("Enter a String : \n");
   fgets(s, MAX_LIMIT, stdin);
   while (s[len] != '[=16=]')
    len++;
   printf("The Entered String is : %s\n", s);
   printf("The Length of Entered String is : %d\n", len);
   return 0;
}

输出:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $gcc -o tryc -Os try.c
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String : 
hello
The Entered String is : hello

The Length of Entered String is : 6
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String : 
hello^[[C
The Entered String is : hello

The Length of Entered String is : 9

这里我们可以清楚地看到它也将 \n 作为字符串的一部分,所以 9

C++

这里也一样..

代码:

#include <iostream>
#include <string>
using namespace std;

int main(){
    string s;
    cout << "Enter a string : " << endl;
    cin >> s;
    cout << "The Entered String is : " << s << endl;
    cout << "The Length of Entered String is : " << s.length() << endl;
    return 0;
}

输出:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $g++ -o trycpp -Os try.cpp
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string : 
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8

Bash

这里也一样.. 代码:

#!/bin/bash  
  
echo "Enter a String : "  
read str
echo "The Entered String is : $str"  
len=`expr length "$str"`
echo "The Length of Entered String is : $len"  

输出:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./try.sh
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./try.sh
Enter a String : 
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8

异常

但这一切都有一个例外

和python.

如果我们直接使用 python 解释器,而不是来自文件的 运行 代码:

这是终端输出:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python
Python 3.9.2 (default, Feb 28 2021, 17:03:44) 
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s = input("Enter a String : \n")
Enter a String : 
hello
>>> print("The Entered String is : " + s)
The Entered String is : hello
>>> print("The Length of Entered String is : " + str(len(s)))
The Length of Entered String is : 5
>>> s = input("Enter a String : \n")
Enter a String : 
hello
>>> print("The Entered String is : " + s)
The Entered String is : hello
>>> print("The Length of Entered String is : " + str(len(s)))
The Length of Entered String is : 5

这里尽管按方向键或退出或回家输出是相同的。

我查看了字符串的内容,使其长度为 8 个字符:

['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']

但这里 [C 也是可打印的!

谁能解释一下这是怎么回事?

我怎样才能摆脱它们?

如果您需要更多详细信息或清晰度,请询问

how it is becoming 8? and when i print them why are they not showing up as '[', 'C', '^' are printable.

您在按向右箭头键时看到的三个 char 组合起来形成一个 转义序列 。第一个字符是 ESC.

ESC 不可打印,但可能会被您的终端消耗,该终端正在进入等待更多内容的状态。该来的就去行动

0x1b  // ESC
0x5b  // [ - CSI - Control Sequence Introducer
0x43  // C - CUF - Cursor Forward

如果您从输出中删除 ESC,您的终端将很乐意打印 [C,但是当前面有 ESC 时,它会形成如上所示的命令。

'\x1b', '[', 'C' 是从键盘发送到 shell 的字符序列,代表右箭头键(光标向前)。

如何在 Java 中避免这些问题:-

如何在 C 或 C++ 中避免这些问题:-

如何在 Python 中避免这些问题:-

如何在 Bash 中避免这些问题:- [如果可以请编辑]