Why does my string not match when reading user input from stdin?

sed -e example
sed examples replace
sed '/^$/d
java scanner string
sed tutorial
sed regex
man sed
sed command -i option

I'm trying to get user input and check if the user put in "y" or "n". Surprisingly, in the below code, neither the if nor the if else case executes! Apparently, correct_name is neither "y" nor "n". How can that be? Am I doing my string conversion wrong or something?

use std::io;

fn main() {
    let mut correct_name = String::new();
    io::stdin().read_line(&mut correct_name).expect("Failed to read line");
    if correct_name == "y" {
        println!("matched y!");
    } else if correct_name == "n" {
        println!("matched n!");
    }
}

read_line includes the terminating newline in the returned string. Add .trim_right_matches("\r\n") to your definition of correct_name to remove the terminating newline.

Custom CGI Scripting with Perl, The following code will print out, line by line, each line in standard input. In this code, the first line reads the first line of the input string and assigns it to $_. Even though we do not specifically see the $_ anywhere in the code, it works in see if the particular muscle group #matches the one the user entered #if they match,  Why does this issue occur? This issue occurs because, when nextInt() method of Scanner class is used to read the age of the person, it returns the value 1 to the variable age, as expected. But the cursor, after reading 1, remains just after it. abc m 1_ // Cursor is here xyz pqr

Instead of trim_right_matches, I'd recommend using trim_right or even better, just trim:

use std::io;

fn main() {
    let mut correct_name = String::new();
    io::stdin().read_line(&mut correct_name).expect("Failed to read line");

    let correct_name = correct_name.trim();

    if correct_name == "y" {
        println!("matched y!");
    } else if correct_name.trim() == "n" {
        println!("matched n!");
    }
}

This last case handles lots of types of whitespace:

Returns a string slice with leading and trailing whitespace removed.

'Whitespace' is defined according to the terms of the Unicode Derived Core Property White_Space.

So Windows / Linux / macOS shouldn't matter.


You could also use the trimmed result's length to truncate the original String, but in this case you should only use trim_right!

let trimmed_len = correct_name.trim_right().len();
correct_name.truncate(trimmed_len);

PHP 5 Advanced: Visual QuickPro Guide, First, the fscanf() function will attempt to read in, from the standard input, one floatingpoint number and one string. These should match up to the temperature (​e.g., 98.6) and the indicator as to the There is no “character” format with fscanf() , so the %s for string will have to do. User input is requested when this script runs. Why does my string not match when reading user input from stdin? How to check if input from stdin is null or newline. 0. C#: Cheking User Input with String Array

You can use the chomp-nl crate which provides a chomp function which returns a string slice without the newline characters.

There is also a trait ChompInPlace if you prefer to do this in-place.

Disclaimer: I am the author of this library.

Beginning Perl for Bioinformatics, User. Input. from. the. Keyboard. You first saw filehandles in Example4-5. a filehandle and the angle bracket input operator are used to read in data from In Example 5-3, a special filehandle called STDIN (short for standard input), is used This newline is not part of the filename and has to be removed before the open​  This is the problem that troubled a lot me too. But now I have founded the way to get rid of it. To take input of a integer we use nextInt() function, that does not read the new line character of your input.

Computer Concepts And C Programming : Holistic Approach To , It is used to read characters, strings and numeric data from the standard input, namely the keyboard. integer, a decimal float, a character, a string and so on in matching arguments. When the user keys the integer value 975, the value 975 is The scanf ( ) statement reads a string which does not consist of blank spaces​. What I have noticed is that if I take integer input from cin (cin >> n;) in the above code before getline, the control does not stop on getline to take str as input from the console. If I don't do (cin >> n) before getline then the control stops on getline and takes the string as input.

perlfaq6 - Regular Expressions, Either you don't have more than one line in the string you're looking at (probably), or else If you want it to happen automatically while reading input, you'll want to set How do I match XML, HTML, or other nasty, ugly things with a regex? In this case, you read the regular expression as user input and store it in $regex . When we say Input, it means to feed some data into a program. An input can be given in the form of a file or from the command line. C programming provides a set of built-in functions to read the given input and feed it to the program as per requirement. When we say Output, it means to display some data on screen, printer, or in any file. C

Scanner (Java Platform SE 8 ), For example, this code allows a user to read a number from System.in: String input = "1 fish 2 fish red fish blue fish"; Scanner s = new Scanner(input). These methods will attempt to match the specified pattern with no regard to delimiters in​  ; scanner. useDelimiter (WHITESPACE_PATTERN); return ch. charAt (0);} catch (NoSuchElementException e) {throw new NoSuchElementException ("attempts to read a 'char' value from standard input, "+ "but no more tokens are available");}} /** * Reads and returns the remainder of the input, as a string. * * @return the remainder of the input, as a

Comments
  • What about Windows, should it be "\r\n"?
  • It tells me bool` is not implemented for the type `&str when I add that
  • I realized it has to go in the if statement after I looked at docs. Thanks!
  • You should not need as_slice, since String implements Deref<Target=str>. let line = io::stdin().read_line(); let trimmed = line.trim_right_chars(..); should work.
  • And I'd suggest using just trim or trim_right instead of trying to worry about cross-platform newlines :-)
  • What benefit / difference is there from trim / trim_right / trim_right_matches?
  • This is using the trim_right_matches function so the chomp function is a shortcut. There is also a trait for doing in-place trimming using truncate thought. So the benefit is mainly to have a shorter way to trim newlines.