How to use substring and substr methods

Overview

There is a Substring method as a function to cut out a specified part of a character string. There is also a charAt method. Cut out an arbitrary length of the character string starting from the specified position.

How to use in Java

public String substring(int Index)
public String substring(int startIndex, int endIndex)

Java's subString method has the above two usages. There are the following usages.

String s1 = "Hello World";
// o World
s1.subString(4)
// O World
s1.subString(4, 11);

How to use in PHP

string substr (String $string, int $start [,int $length ] )

string Input string. At least one character must be specified.

start If start is positive, the returned string is a string starting from the start, counting from 0 in String. If start is negative, the returned string is the string starting from the start, counting from the end of the String.

length If length is positive, the returned string will be the number of characters in length counting from start. If length is negative, the character for that string is omitted from the end of string.

<?php
$rest = substr("Hello World", -1); // "d"return it
$rest = substr("v", -2); // "ld"return it
$rest = substr("Hello World", -3, 1); // "r"return it
?>

Summary

I use Java for business, so I understand it to some extent, but I've never heard of PHP's substr method. Therefore, I wrote it in detail.

Recommended Posts

How to use substring and substr methods
How to use StringBurrer and Arrays.toString.
How to use EventBus3 and ThreadMode
How to call classes and methods
How to use equality and equality (how to use equals)
How to use class methods [Java]
How to use OrientJS and OrientDB together
How to set up and use kapt
How to use @Builder and @NoArgsConstructor together
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use FileReader class and BufferedReader class
[Ruby] How to use gsub method and sub method
How to use scope and pass processing (Jakarta)
How to access Java Private methods and fields
[Java] How to use Calendar class and Date class
[Java] How to use Map
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use JUnit (beginner)
How to use Ruby return
[Rails] How to use enum
[Swift] How to use UserDefaults
How to use java class
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Ruby: How to use cookies
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use RealSense with ubuntu 20.04 and ROS Noetic
How to use Java variables
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"