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

2024-10-02 08:26:05 发布

您现在位置:Python中文网/ 问答频道 /正文

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

[注:]

我添加了许多编程语言的详细信息,因为在这个问题上,所有编程语言的问题都是一样的,我在这个问题上唯一的重点是如何克服这个问题,并从程序本身获得真正的终端体验

首先,

爪哇

代码:

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会出现,而不是光标移动(其他箭头键、escape键、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] != '\0')
    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] != '\0')
    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作为字符串so9的一部分

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

猛击

这里也一样。。 代码:

#!/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

在这里,尽管按了箭头键或escape或home,输出仍然相同

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

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

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

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

我怎样才能摆脱它们呢

如果您需要更多的细节或清晰度,请询问


Tags: ofthe代码hellostringlenislength
2条回答

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中避免这些问题:-https://stackoverflow.com/a/66901865/14911094

如何避免在C或C++中:-^ https://stackoverflow.com/a/66886788/14911094

如何在Python中避免这些问题:-https://stackoverflow.com/a/66895907/14911094

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

相关问题 更多 >

    热门问题