Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在不同的编程语言中带有奇怪字符的标准输入_Java_Python_C++_C_Stdin - Fatal编程技术网

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

Java 在不同的编程语言中带有奇怪字符的标准输入,java,python,c++,c,stdin,Java,Python,C++,C,Stdin,我对这些编程语言的标准输入感到困惑: [注:] 我添加了许多编程语言的详细信息,因为在这个问题上,所有编程语言的问题都是一样的,我在这个问题上唯一的重点是如何克服这个问题,并从程序本身获得真正的终端体验 首先, JAVA 代码: import java.util.Scanner; public class Try{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); Syst

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

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

首先,

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
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
#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
#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
输出:

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
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
#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
#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
当我按箭头键
^[[C
显示,而不是光标移动时(其他箭头键、escape键、home键、end键也会出现类似情况)

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

python 这里和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
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
#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
#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
输出:

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
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
#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
#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
C 这里也一样

代码:

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
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
#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
#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
但代码略有不同:

代码:

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
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
#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
#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
这里我们可以清楚地看到,
\n
也是字符串so 9的一部分

C++ 这里也一样

代码:

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
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
#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
#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
猛击 这里也一样。。 代码:

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
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
#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
#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
输出:

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
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
#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
#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
也是可打印的

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

我怎样才能摆脱它们呢

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

它是如何变成8的?当我打印它们时,为什么它们不显示为“[”、“C”、“^”是可打印的

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

ESC
不可打印,但可能会被您的终端使用,而您的终端正处于等待更多信息的状态。当它出现时,它会对其进行操作

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

如果您从输出中删除
ESC
,您的终端将很乐意打印
[C
,但在
ESC
之前,它会形成一个如上所示的命令。

字符序列“\x1b”、“[”、“C”是从键盘发送到外壳的字符序列,用于表示右箭头键(光标向前)

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

如何避免在C或C++中:-

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


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

我认为这些字符存在,但无法打印。如果您逐个字母打印单词并打印它们的数值,我认为您将看到“缺失” characters@GalNo check我更新了说明stringRelated的值的问题:。Python也使用自己的shell,因此它处理自己的输入,并可能隐藏不可打印的控件字符,您希望发生什么以及为什么?按“up”这样做根本没有意义,在某些情况下,基本上所有的处理方法都是错误的。我明白了,所以你希望用户能够在程序输入行上移动光标?有解决方案,但每种语言都有具体的解决方案。我知道C语言有一种解决方案,但所有语言一次都是一个太宽泛的问题。我们如何克服它?是吗你的意思是如何从输入中剥离转义序列?你可以使用像
ncurses
pdcurses
这样的库,或者一些能够解释这些序列的平台特定库。它们并不总是具有相同的长度,从头开始实现一个健壮的解释器可能需要大量的时间和精力,特别是因为不同的终端会发射和理解不同的转义序列,为什么使用Python解释器来解释这个异常呢?它可能内置了这样的解释器。当以交互模式运行它时,你应该能够使用箭头来获取上一行C++。inal将发送转义序列。您可以使用库直接解释它们,也可以直接从您读取的内容中丢弃控制字符,以便用户获得
[C
,而不是“不可见”序列。