Snippets tagged “string”
63 snippets use this tag.
- Add leading zeroes to number in Java?Java
To add leading zeroes to a number in Java, you can use the format method of the String class, along with a format string that specifies the desired length and padding character.
- Allow only [a-z][A-Z][0-9] in string using PHPPHP
You can use the preg_replace function in PHP to remove any characters that do not match the specified pattern.
- Alphabet range in PythonPython
In Python, you can use the string module's ascii_lowercase and ascii_uppercase constants to get the range of lowercase and uppercase letters in the ASCII character set respectively.
- Check and extract a number from a String in JavaJava
To check if a string contains a number and extract the number from the string in Java, you can use a combination of the matches method of the String class and the find method of the Matcher class.
- Check whether a String is not Null and not EmptyJava
To check if a string is not null and not empty in Java, you can use the length() method of the java.lang.String class to check if the string is empty, and the != operator to check if the string is not null. Here is an example of how you can do this:
- Convert a String representation of a Dictionary to a dictionaryPython
You can use the json module in Python to convert a string representation of a dictionary to a dictionary.
- Convert float to String and String to float in JavaJava
To convert a float to a String in Java, you can use the Float.toString method or the String.valueOf method. For example:
- Convert java.util.Date to StringJava
To convert a java.util.Date object to a String, you can use the format method of the SimpleDateFormat class from the java.text package. The SimpleDateFormat class provides a convenient way to convert a Date object to a String based on a specified format.
- Convert String array to ArrayListJava
To convert a String array to an ArrayList in Java, you can use the following steps:
- Converting A String To Hexadecimal In JavaJava
To convert a string to a hexadecimal string in Java, you can use the encodeHexString method of the org.apache.commons.codec.binary.Hex class.
- Difference between StringBuilder and StringBufferJava
The StringBuilder and StringBuffer classes in Java are used to create mutable strings. A mutable string is a string that can be modified after it is created, unlike a regular string, which is immutable and cannot be modified.
- Explanation of 'String args[]' and static in 'public static void main(String[] args)'Java
In the main() method in Java, String args[] is an array of strings that holds the command-line arguments passed to the program.
- How can I check if a single character appears in a string?Java
To check if a single character appears in a string in Java, you can use the indexOf method of the String class.
- How can I clear or empty a StringBuilder?Java
To clear or empty a StringBuilder in Java, you can use the setLength method and set the length to 0.
- How can I convert a stack trace to a string?Java
To convert a stack trace to a string in Java, you can use the printStackTrace() method of the Throwable class, which prints the stack trace to a PrintWriter.
- How can I pad a String in Java?Java
To pad a string in Java, you can use the String.format() method and specify a width for the string.
- How do I convert a String to an InputStream in Java?Java
To convert a String to an InputStream in Java, you can use the ByteArrayInputStream class, which allows you to create an InputStream from a byte array.
- How do I convert a String to an int in Java?Java
To convert a String to an int in Java, you can use the parseInt() method of the Integer class. Here's an example:
- How do I convert from int to String in Java?Java
To convert an int to a String in Java, you can use the Integer.toString() method. For example
- How do I create a Java string from the contents of a file?Java
To create a Java string from the contents of a file, you can use the following code:
- How do I get a platform-dependent new line character?Java
To get a platform-dependent new line character in Java, you can use the System.lineSeparator() method.
- How do I get the file extension of a file in Java?Java
To get the file extension of a file in Java, you can use the File class and its getName() method to get the file name, and then use the substring() method of the String class to extract the extension from the file name.
- How do I get the file name from a String containing the Absolute file path?Java
To get the file name from a String containing the absolute file path in Java, you can use the following methods:
- How do I save a String to a text file using Java?Java
To save a String to a text file in Java, you can use the write method of the FileWriter class. Here's an example of how you can do this:
- How to check if a String is numeric in JavaJava
To check if a String is numeric in Java, you can use the isNumeric method of the StringUtils class from the org.apache.commons.lang3 library.
- How to Check Whether a String Matches a RegEx in JavaScriptJavaScript
Read and learn two methods used to check whether a string matches RegEx in JavaScript. Also, see which method is the fastest and differences between them.
- How to convert a char array back to a string?Java
To convert a char array back to a String in Java, you can use the String class's constructor that takes a char array as an argument.
- How to convert an int array to String with toString method in JavaJava
To convert an int array to a string in Java, you can use the Arrays.toString() method from the java.util package.
- How to Convert DateTime to String in PHPPHP
If you wish to manipulate with the DateTime object in PHP, then this snippet is for you. Here, you will learn how to correctly convert DateTime to string.
- How to convert Java String into byte[]?Java
To convert a Java string into a byte array, you can use the getBytes() method of the java.lang.String class. This method returns an array of bytes representing the string in a specific encoding.
- how to convert java string to Date objectJava
To convert a string to a Date object in Java, you can use the parse() method of the SimpleDateFormat class.
- How to convert String object to Boolean Object?Java
To convert a String object to a Boolean object in Java, you can use the Boolean.valueOf method:
- How to convert String to long in Java?Java
To convert a string to a long in Java, you can use the parseLong() method of the java.lang.Long class. This method parses the string as a signed decimal long, and returns the resulting long value.
- How to convert/parse from String to char in java?Java
In Java, you can convert a string to a character using the charAt() method of the String class. This method takes an index as an argument and returns the character at that index.
- How to create JSON Object using String?Java
To create a JSON object from a string in Java, you can use the org.json library. Here's an example of how to do this:
- How to evaluate a math expression given in string form?Java
There are a few different ways to evaluate a math expression given in string form:
- How to Get a File Extension in PHPPHP
Here, we have collected five simple and efficient ways that will help you to get a PHP file extension. Check out the examples and choose the best one.
- How to get a function name as a string?Python
In Python, you can use the built-in function attribute __name__ to get the name of a function as a string.
- How to get current timestamp in string format in Java? "yyyy.MM.dd.HH.mm.ss"Java
To get the current timestamp in string format in Java, you can use the SimpleDateFormat class and specify the desired format string.
- How to hash some String with SHA-256 in Java?Java
To hash a string using SHA-256 in Java, you can use the MessageDigest class from the java.security package.
- How to remove line breaks from a file in Java?Java
There are several ways to remove line breaks from a file in Java.
- How to remove single character from a String by indexJava
To remove a single character from a string by its index in Java, you can use the StringBuilder.deleteCharAt() method. Here's an example of how to use it:
- How to Split a String in JavaJava
Learn the ways to split a string in Java. The most common way is using the String.split () method, also see how to use StringTokenizer and Pattern.compile ().
- Java - removing first character of a stringJava
To remove the first character of a string in Java, you can use the substring() method of the String class.
- Java Byte Array to String to Byte ArrayJava
To convert a byte array to a string and back to a byte array in Java, you can use the getBytes method of the String class and the getBytes method of the Charset class.
- Java Does Not Equal (!=) Not Working? [duplicate]Java
It is possible that the != operator is not working as expected in your Java code because you are using it to compare object references, rather than the contents of the objects themselves.
- Java int to String - Integer.toString(i) vs new Integer(i).toString()Java
In Java, there are several ways to convert an int value to a String:
- Java string to date conversionJava
To convert a string to a date in Java, you can use the parse method of the SimpleDateFormat class.
- Java: convert List<String> to a join()d StringJava
To convert a List<String> to a single string by joining the elements of the list with a separator, you can use the join() method of the java.util.StringJoiner class.
- Left padding a String with ZerosJava
To left pad a String with zeros in Java, you can use the String.format() method and the %0Nd format specifier, where N is the total length of the padded string.
- PHP convert string to hex and hex to stringPHP
To convert a string to its hexadecimal representation, you can use the bin2hex function in PHP.
- php variable in html no other way than: <?php echo $var; ?>PHP
Yes, that is one way to output the value of a PHP variable in HTML.
- Random string generation with upper case letters and digitsPython
Here is a simple function that generates a random string of a given length using upper case letters and digits:
- SimpleXml to stringPHP
To convert a SimpleXml object to a string in PHP, you can use the asXML() function.
- Split Java String by New LineJava
To split a string in Java by new line, you can use the split method of the String class and pass it the regular expression "\n" or "\r\n", depending on the platform you are running on.
- String concatenation: concat() vs "+" operatorJava
In Java, you can use the concat() method of the java.lang.String class or the + operator to concatenate (join) two or more strings.
- string to string array conversion in javaJava
There are several ways to convert a string to a string array in Java. Here are a few options:
- String.equals versus ==Java
In Java, the == operator is used to compare the primitive values of two variables, while the equals() method is used to compare the contents of two objects.
- Take a char input from the ScannerJava
To take a character input from the Scanner in Java, you can use the next() method to read a string and then use the charAt(int index) method to get the first character of that string. Here's an example:
- What is "String args[]"? parameter in main method JavaJava
String args[] is a parameter in the main method of a Java program. It is an array of strings that can be used to pass command-line arguments to the program when it is executed.
- What is simplest way to read a file into String?Java
To read a file into a String in Java, you can use the readAllBytes() method of the Files class and the new String() constructor:
- What's the simplest way to print a Java array?Java
There are several ways to print an array in Java. Here are a few options: Using a loop
- Why can't I use switch statement on a String?Java
In the Java programming language, you can use a switch statement to choose between a fixed number of alternatives.