QuizCure
Find Available Solution Here!

String

QuizCure Member Mar 19, 2023

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 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 baseValue 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: Same we can do for 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 towards 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 see how as 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 method widely used in java to compare strings as below:

  • ==
  • equals()
  • compareTo()

Let’s understand in following tabular form

== equals() compareTo()
Operator Java String method Java String method
Use for Use to compare object references. It check if both strings pointing to same memory address. Use to compare content having same characters sequence. Use to compare strings lexicographically (mean it 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 string having same value arranged in alphabetical order.

Beginners mostly do mistake while comparing strings params and use == operator instead of equals method Hope with above explanations it would be clear what need to use to compare strings based on scenario.

Was this post helpful?

Send Feedback

Connect With QuizCure


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

Contributed By

QuizCure Member
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO