string interview questions
Top string frequently asked interview questions
In Swing, the password field has a getPassword()
(returns char[]
) method instead of the usual getText()
(returns String
) method. Similarly, I have come across a suggestion not to use String
to handle passwords.
Why does String
pose a threat to security when it comes to passwords?
It feels inconvenient to use char[]
.
Source: (StackOverflow)
How can I check if a string ends with a particular character in JavaScript?
Example: I have a string
var str = "mystring#";
I want to know if that string is ending with #
. How can I check it?
Is there a endsWith()
method in JavaScript?
One solution I have is take the length of the string and get the last character and check it.
Is this the best way or there is any other way?
Source: (StackOverflow)
How can I check if one string contains another substring in JavaScript?
Usually, I would expect a String.contains()
method, but there doesn't seem to be one. What is a reasonable way to check for this?
Source: (StackOverflow)
I'm looking for a string.contains
or string.indexof
method in Python.
I want to do:
if not somestring.contains("blah"):
continue
Source: (StackOverflow)
I have the following code in Ruby. I want to convert this code into JavaScript. what's the equivalent code in JS?
text = <<"HERE"
This
Is
A
Multiline
String
HERE
Source: (StackOverflow)
console.log("double");
vs console.log('single');
I see more and more JavaScript libraries out there using single quotes when handling strings. What are the reasons to use one over the other? I thought they're pretty much interchangeable.
Source: (StackOverflow)
In C#, what is the difference between String
and string
? (note the case)
Example:
string s = "Hello world!";
String S = "Hello world!";
What are the guidelines for the use of each? And what are the differences?
Source: (StackOverflow)
If you have java.io.InputStream
object, how should you process that object and produce a String
?
Suppose I have an InputStream
that contains text data, and I want to convert this to a String
(for example, so I can write the contents of the stream to a log file).
What is the easiest way to take the InputStream
and convert it to a String
?
public String convertStreamToString(InputStream is) {
// ???
}
Source: (StackOverflow)
Is there a way to make the following return true?
string title = "ASTRINGTOTEST";
title.Contains("string");
There doesn't seem to be an overload that allows me to set the case sensitivity.. Currently I UPPERCASE them both, but that's just silly.
UPDATE
The sillyness I refer to is the i18n issues that come with up- and down casing.
UPDATE
This question is ancient and since then I have realized I asked for a simple answer for a really vast and difficult topic if you care to investigate it fully.
For most cases, in mono-lingual, English code bases this answer will suffice. I'm suspecting because most people coming here fall in this category this is the most popular answer.
This answer however brings up the inherent problem that we can't compare text case insensitive until we know both texts are the same culture and we know what that culture is. This is maybe a less popular answer, but I think it is more correct and that's why I marked it as such.
Source: (StackOverflow)
How do I convert a string
to a byte[]
in .NET (C#)?
Also, why should encoding be taken into consideration? Can't I simply get what bytes the string has been stored in? Why is there a dependency on character encodings?
Source: (StackOverflow)
This has always confused me. It seems like this would be nicer:
my_list = ["Hello", "world"]
print my_list.join("-")
# Produce: "Hello-world"
Than this:
my_list = ["Hello", "world"]
print "-".join(my_list)
# Produce: "Hello-world"
Is there a specific reason it does it like this?
Source: (StackOverflow)
How do I read every line of a file in Python and store each line as an element in an array?
I want to read the file line by line and each line is appended to the end of the array.
Source: (StackOverflow)
The following print statement would print "hello world".
Could anyone explain this?
System.out.println(randomString(-229985452) + " " + randomString(-147909649));
And randomString()
looks like this:
public static String randomString(int i)
{
Random ran = new Random(i);
StringBuilder sb = new StringBuilder();
while (true)
{
int k = ran.nextInt(27);
if (k == 0)
break;
sb.append((char)('`' + k));
}
return sb.toString();
}
Source: (StackOverflow)