How to convert CharSequence to String in Java?

How to convert CharSequence to String in Java?

Asked on October 26, 2018 in String.
Add Comment


  • 3 Answer(s)

      It is possible by finding the position of the first string with string’s find() member, and then replace with it’s replace() member.

    string s("hello $name");
    size_type pos = s.find( "$name" );
    if ( pos != string::npos )
    {
      s.replace( pos, 5, "somename" ); // 5 = length( $name )
    }
    
    Answered on October 26, 2018.
    Add Comment

      Use directly the  String.valueOf()

    String.valueOf(charSequence)
    

      This is useful when a method can return either a charSequence or null value

    Answered on October 26, 2018.
    Add Comment

       Invoking the toString() method.

    • Returns a string containing the characters in this sequence in the same order as this sequence.
    • The length of the string will be the length of this sequence.
    Answered on October 26, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.