QuizCure
Find Available Solution Here!

String

Deepak Apr 23, 2024

A string is a data type that represents text rather than numbers. It is a sequence of valid characters which can include spaces, alphanumeric characters, punctuation, Newline Character, and other valid upper and lower case character.

Example of String are as follows:

  • "IamString!"
  • "Hello123"
  • "email@emaildomain.com"
  • "Hello there"

How to convert given string into int (number) value in php?

There are various ways in PHP to convert string to int. Let us understand one by one

Convert string to int using php function intval()

Syntax : inval($valueNeedtoConvert, $baseVal);

Where the default value of $baseVal is 10 (decimal). Therefore it will convert all passed string variables to int by assuming as decimal by default.

<?php
/**
 *  String to int conversion
 */

$strVar = "50";

var_dump($strVar);
// Output string(2) "50"


var_dump(gettype($strVar));
// Output String

//Convert String variable to interger
$intVar = intval($strVar);

var_dump(gettype($intVar));
// Output ineteger

var_dump($intVar);
// Output int(50)

Convert Octal to int using php function intval()

<?php
/**
 *  String (Octal) to int conversion
 */

$strVar = "051";
// Octal Start with 0


//Convert String variable in octal to decimal number
$intVar = intval($strVar, 8);
// MUST provide base value as 8 to let intalVal function know about passed variable type as octal.

var_dump($intVar);
// Output int(41)


How 41?
051 (Octal) => 5*81 + 1*80 (41 Decimal) Note: We can do Hex to decimal conversion by using baseValue as 16.

Convert string to int using Type Casting

Syntax: $intVar = (int)$strVar;

$strVar = "7.9999999999";

var_dump(gettype($strVar)); // Outputs: string



// Cast to integer
$intVar = (int)$strVar;



var_dump(gettype($intVar)); // Outputs: integer



var_dump($intVar); // Outputs: 7


Note: It will be rounded toward zero for float-to-int conversion.

How to convert given string into int (number) value in Java?

Method parseInt(String s), a part of wrapper class Integer can used to convert string to integer value. Let's see how as the example below


package javaapplication;


public class JavaApplication {


    public static void main(String[] args) {

        //Declaring String variable  
        String str = "750";
        
       //Converting String 750 into int using Integer.parseInt()  

        int i = Integer.parseInt(str);
 
        System.out.println(i);
       // Output: 750
        
    } 

}

How to compare equality of two strings in java?

Here is the list of comparison methods widely used in Java to compare strings as below:

  • ==
  • equals()
  • compareTo()

Let's understand in the following tabular form

== equals() compareTo()
Operator Java String method Java String method
Use for Use to compare object references. It checks if both strings pointing to the same memory address. Use to compare content having the same character sequence. Use to compare strings lexicographically (mean it is used to compare strings in alphabetically ordered).
Example 1: (Compare two string abc)

System.out.println("abc" == "abc" );

Output : true

Reason: Since both string abc point to the same memory address.;

System.out.println("abc".equals("abc") );

Output: true

Reason: Because both string have same sequence of character

System.out.println("abc".compareTo("abc") );

Output: 0

Reason: Because both are in the same alphabetically order.

Example 2: Compare two string defined as

String strOne = "hello";

String strTwo = "hello";

System.out.println(strOne == strTwo);

Output : true

Reason: Since both string abc point to the same memory address.

System.out.println(strOne.equals(strTwo) );

Output: true

Reason: Because both string have same sequence of character

System.out.println(strOne.compareTo(strTwo) );

Output: 0

Reason: Because both are in the same alphabetically order.

Example 3 Compare two string defined as

String strOne = "hello";

String strTwo = new String("hello");

System.out.println(strOne == strTwo);

Output : false

Reason: Because by using a new operator it creates a new object (new memory address) for value hello. Therefore being different object references == return false.

System.out.println(strOne.equals(strTwo) );

Output: true

Reason: Because both string have same sequence of characte

System.out.println(strOne.compareTo(strTwo) );

Output: 0

Reason: Because both strings have the same value arranged in alphabetical order.

Beginners mostly make mistake while comparing string params and using == operator instead of the equals method Hope with the above explanations it will be clear what need to be used to compare strings based on the scenario.

Was this post helpful?

Send Feedback

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO
Scroll up