EzDevInfo.com

string.js

Extra JavaScript string methods. string.js

startsWith() and endsWith() functions in PHP

How can I write two functions that would take a string and return if it starts with the specified character/string or ends with it?

For example:

$str = '|apples}';

echo startsWith($str, '|'); //Returns true
echo endsWith($str, '}'); //Returns true

Source: (StackOverflow)

String contains in bash

Using bash, I have a string:

string=`echo My string`

How can I test if it contains another string?

if [ $string ?? 'foo' ] then;
  echo "It's there!";
fi;

Where ?? is my unknown operator. Do I use echo and grep?

if [ `echo $string | grep 'foo' ` ] then;
  echo "It's there!";
fi;

That looks a bit clumsy.


Source: (StackOverflow)

Advertisements

What's the difference between String and string?

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)

Converting String to int in Java?

How can a String be converted to an int in Java?

My String contains only numbers and I want to return the number it represents.

For example, given the string "1234" the result should be the number 1234.


Source: (StackOverflow)

Check if string contains specific words?

Consider:

$a = 'How are you?';

if ($a contains 'are')
    echo 'true';

Suppose I have the code above, what is the correct way to write the statement if ($a contains 'are')?


Source: (StackOverflow)

Why does this code using random strings print "hello world"?

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)

Why is char[] preferred over String for passwords in Java?

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)

Is there a way to substring a string in Python?

Is there a way to substring a string in Python, to get a new string from the 3rd character to the end of the string?

Maybe like myString[2:end]?

If leaving the second part means 'till the end', if you leave the first part, does it start from the start?


Source: (StackOverflow)

Does Python have a string contains method?

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)

Trim string in JavaScript?

How do I trim a string in JavaScript?


Source: (StackOverflow)

Creating multiline strings in JavaScript

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)

Case insensitive 'Contains(string)'

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)

Convert JavaScript String to be all lower case?

How can I convert a JavaScript string value to be in all lower case letters?

Example: "Your Name" to "your name"


Source: (StackOverflow)

Extract filename and extension in Bash

I want to get the filename (without extension) and the extension separately.

The best solution I found so far is:

NAME=`echo "$FILE" | cut -d'.' -f1`
EXTENSION=`echo "$FILE" | cut -d'.' -f2`

This is wrong because it doesn't work if the file name contains multiple "." characters. If, let's say, I have a.b.js it will consider a and b.js, instead of a.b and js.

It can be easily done in Python with

file, ext = os.path.splitext(path)

but I'd prefer not to fire a Python interpreter just for this, if possible.

Any better ideas?


Source: (StackOverflow)

Read/convert an InputStream to a String

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)