(Java, JavaScript, Python) Comparison of string processing

Java, JavaScript, Python processing comparison

I recently started studying JavaScript and Python. As a summary of what I learned, I would like to write an article comparing the processing of Java, JavaScript, and Python. This time it is character string processing.

Comparison list of character string processing

Processing content Java JavaScript Python
String length length() length len(String)
Match comparison equals(Comparison character)
equalsIgnoreCase(Comparison character)
==Comparison operator
===Comparison operator
==Comparison operator
Search indexOf(Search文字列)
lastIndexOf(Search文字列)
contains(Search文字列)
startsWith(Search文字列)
endsWith(Search文字列)
indexOf(Search文字列)
lastIndexOf(Search文字列)
includes(Search文字列)
startsWith(Search文字列)
endsWith(Search文字列)
find(Search文字列)
rfind(Search文字列)
in expression
not in expression
startswith(Search文字列)
endswith(Search文字列)
Simple join + Operator
concat(Concatenation string)
StringBuilder.append(Concatenation string)
+ Operator
concat(Concatenation string,...)
+ Operator
White space removal trim()
strip()
trim()
trimLeft()
trimRight()
strip(Character to be deleted)
lstrip(Character to be deleted)
rstrip(Character to be deleted)
Case conversion toUpperCase()
toLowerCase()
toUpperCase()
toLowerCase()
upper()
lower()
capitalize()
title()
swapcase()
Cut out substring(Start index,End index)
charAt(index)
substring(Start index,End index)
charAt(index)
slice(Start index,End index)
substr(Start index,word count)
String[Start index:End index]
repetition repeat(Number of repetitions) repeat(Number of repetitions) *operator
Replacement replace(Preamble,After character)
replaceFirst(Preamble,After character)
replaceAll(Preamble,After character)
replace(Preamble,After character)
replaceAll(Preamble,After character)
replace(Preamble,After character)
re.sub(Preamble,After character,String)
re.subn(Preamble,After character,String)
translate(formula)
Combine by delimiter String.join(Delimiter,Concatenation string...) Combining character array.join(Delimiter) Delimiter.join(Combining character array)
Divide by delimiter split(Delimiter)
split(Delimiter) split(Delimiter)
rsplit(Delimiter)
splitlines()
re.split(Delimiter)
Arbitrary molding String.format(Molding type,String...) Template literal →
`xxx${Variable name}xxx`
Molding type.format(Variables and expressions...)
f'xxx{Variables and expressions}xxx'
Formation formula% (Variables and expressions,...)

Postscript

--Match comparison --Java equalsIgnoreCase () performs case-insensitive match comparisons. -=== The operator makes a strict comparison including the type. '123'=== 123 → false --Search --Returns the character index of the match: indexOf (), lastIndexOf (), find (), rfind () --Returns true/false: contains (), includes (), in expression, not in expression --Match judgment of first character, return true/false: startsWith () --Match judgment of last character, return true/false: endsWith () --Blank removal --In both languages, single-byte spaces, tabs, and line breaks are removed. --In addition to the above, Java strip () also removes double-byte spaces. --The Python function can specify the character to be deleted as an argument. If omitted, spaces, tabs, and line breaks will be deleted. --Category conversion --Python has more detailed conversion functions than the other two. capitalize () → convert first letter to uppercase and others to lowercase, title () → word first letter to uppercase and others to lowercase, swapcase () → swap case --Cut out --substring () can omit the end index, in which case the end of the string is the end index. --JavaScript cutout substring () and slice () are the same in normal usage, but the result will be different if the arguments are irregular. --If start index> end index, substring () will automatically replace the start and end, and slice () will return an empty string. --If a negative number is specified, substring () treats the argument as 0, and slice () treats it as "string length-argument". --Python uses slices of the form "string [start: end]" instead of functions. Either start or end can be omitted. If ":" is omitted, one character is acquired like charAt (). --Replace --Java: replace () replaces all matched characters, replaceFirst () replaces the first matched character. --JavaScript: replace () replaces the first matched character, replaceAll () replaces all matched characters, but replace () can also replace all characters with the regular expression option "g". --Python: replace () can specify the maximum number of substitutions in the third argument like split (). --Python: translate () can replace multiple cases. However, the replacement source character must be one character. Specify the replacement combination with "str.maketrans ()". "" AbCdefghIk ".translate (str.maketrans ({'C':'c','I':'ij'}))" --Regular expressions can be used in each language as follows. - Java:replaceFirst()、replaceAll()
- JavaScript:replace()、replaceAll()
--Python: re.sub (), re.subn (), subn returns a replaced string + a list of converted numbers (to be exact, tuples). --Split by delimiter --Split () can specify the maximum number of splits in the second argument in any language. --Regular expressions can be used for split () in Java and JavaScript. In JavaScript, the space delimiter is specified as "/ \ s /" with a regular expression. --For Python, the re module re.split () can use regular expressions. --The argument of split () in Python can be omitted. If omitted, spaces, tabs, and line breaks will be split. --Python's rsplit () is different from split () in counting from the right when the maximum number of divisions is specified in the second argument. --Python splitlines () splits with line breaks. It also supports OS-dependent line breaks. --Arbitrary molding --Python has several formats. See the sample below for format (). --One is% operator: sei ='Yamada' mei ='Taro', print ('name:% s% s'% (sei, mei)) --One is f string: print (f'name: {sei} {mei}')

sample

Java

public class StringSample {

    public static void main(String[] args) {
        StringSample obj = new StringSample();
        obj.executeSample();
    }

    public void executeSample() {
        String str = "Yamada,Taro,Yamada,Taro,M,19950827,25 years old,2 Kanda Sakumacho, Chiyoda-ku, Tokyo-XX-XX〇〇so";

        //White space removal
        String strKuhaku1 = str.trim();

        //Divide by delimiter
        String[] strBun1 = strKuhaku1.split(",");

        //Simple join
        String resName = new StringBuilder().append(strBun1[0]).append(strBun1[1]).toString();

        //Case conversion
        String strOoko1 = strBun1[2].toUpperCase();
        String strOoko2 = strBun1[3].toUpperCase();

        //Simple join
        String resAlpha = strOoko1.concat(" ").concat(strOoko2);

        //Match comparison
        String resSex;
        if (strBun1[4].equals("M")) {
            resSex = "male";
        } else {
            resSex = "Female";
        }

        //Cut out
        String strKiri1 = strBun1[5].substring(0, 4);
        String strKiri2 = strBun1[5].substring(4, 6);
        String strKiri3 = strBun1[5].substring(6);

        //Combine by delimiter
        String resBirth = String.join("/", strKiri1, strKiri2, strKiri3);

        //Replacement
        String resAge = strBun1[6].replace("Talent", "age");

        //String length
        String resAddress = strBun1[7];
        if (16 < strBun1[7].length()) {
            resAddress = strBun1[7].substring(0, 16);
            //Simple join
            resAddress = resAddress + "(Abbreviation)";
        }

        //Search
        String resMessage1 = "";
        String resMessage2 = "";
        if (strBun1[7].startsWith("Tokyo")) {
            resMessage1 = "It is the capital.";
        }
        if (strBun1[7].contains("Chiyoda Ward")) {
            resMessage2 = "There is Tokyo station.";
        }

        //Arbitrary molding
        String res1 = String.format("%s(%s)Mr%s %s raw(%s)", resName, resAlpha, resSex, resBirth, resAge);
        String res2 = String.format("%s %s%s", resAddress, resMessage1, resMessage2);

        //repetition
        String line = "-".repeat(64);

        System.out.println(line);
        System.out.println(res1);
        System.out.println(res2);
        System.out.println(line);
    }
}

JavaScript

<html>
<head>
<script>
function executeSample() {
        let str = "Yamada,Taro,Yamada,Taro,M,19950827,25 years old,2 Kanda Sakumacho, Chiyoda-ku, Tokyo-XX-XX〇〇so";

        //White space removal
        let strKuhaku1 = str.trim();

        //Divide by delimiter
        let strBun1 = strKuhaku1.split(",");

        //Simple join
        let resName = strBun1[0] + strBun1[1];

        //Case conversion
        let strOoko1 = strBun1[2].toUpperCase();
        let strOoko2 = strBun1[3].toUpperCase();

        //Simple join
        let resAlpha = strOoko1.concat(" ", strOoko2);

        //Match comparison
        let resSex;
        if (strBun1[4] == "M") {
            resSex = "male";
        } else {
            resSex = "Female";
        }

        //Cut out
        let strKiri1 = strBun1[5].substring(0, 4);
        let strKiri2 = strBun1[5].substring(4, 6);
        let strKiri3 = strBun1[5].substring(6);

        //Combine by delimiter
        let resBirth = [strKiri1, strKiri2, strKiri3].join("/");

        //Replacement
        let resAge = strBun1[6].replace("Talent", "age");

        //String length
        let resAddress = strBun1[7];
        if (16 < strBun1[7].length) {
            resAddress = strBun1[7].substring(0, 16);
            //Simple join
            resAddress = resAddress + "(Abbreviation)";
        }

        //Search
        let resMessage1 = "";
        let resMessage2 = "";
        if (strBun1[7].startsWith("Tokyo")) {
            resMessage1 = "It is the capital.";
        }
        if (strBun1[7].includes("Chiyoda Ward")) {
            resMessage2 = "There is Tokyo station.";
        }

        //Arbitrary molding
        let res1 = `${resName}(${resAlpha})Mr${resSex} ${resBirth}Living(${resAge})`;
        let res2 = `${resAddress} ${resMessage1}${resMessage2}`;

        //repetition
        let line = "-".repeat(64);

        console.log(line);
        console.log(res1);
        console.log(res2);
        console.log(line);
    }
</script>
</head>
<body onload="executeSample()">
  <h1>Qiita article</h1>
  <h2>String processing</h2>
</body>
</html>

Python

str1 = "Yamada,Taro,Yamada,Taro,M,19950827,25 years old,2 Kanda Sakumacho, Chiyoda-ku, Tokyo-XX-XX〇〇so"

#White space removal
strKuhaku1 = str1.strip()

#Divide by delimiter
strBun1 = strKuhaku1.split(",")

#Simple join
resName = strBun1[0] + strBun1[1]

#Case conversion
strOoko1 = strBun1[2].upper()
strOoko2 = strBun1[3].upper()

#Simple join
resAlpha = strOoko1 + " " + strOoko2

#Match comparison
resSex = ""
if strBun1[4] == "M":
    resSex = "male"
else:
    resSex = "Female"

#Cut out
strKiri1 = strBun1[5][0:4]
strKiri2 = strBun1[5][4:6]
strKiri3 = strBun1[5][6:]

#Combine by delimiter
resBirth = "/".join([strKiri1, strKiri2, strKiri3])

#Replacement
resAge = strBun1[6].replace("Talent", "age")

#String length
resAddress = strBun1[7]
if 16 < len(strBun1[7]):
    resAddress = strBun1[7][:16]
    #Simple join+
    resAddress = resAddress + "(Abbreviation)"

#Search
resMessage1 = ""
resMessage2 = ""
if strBun1[7].startswith("Tokyo"):
    resMessage1 = "It is the capital."
if  "Chiyoda Ward" in strBun1[7]:
    resMessage2 = "There is Tokyo station."

#Arbitrary molding
res1 = "{}({})Mr{} {}Living({})".format(resName, resAlpha, resSex, resBirth, resAge)
res2 = "{} {}{}".format(resAddress, resMessage1, resMessage2)

#repetition*
line = "-" * 64

print(line)
print(res1)
print(res2)
print(line)

Output result

----------------------------------------------------------------
Taro Yamada(YAMADA TARO)Mr. Male 1995/08/27 students(25 years old)
2 Kanda Sakumacho, Chiyoda-ku, Tokyo-X(Abbreviation)It is the capital. There is Tokyo station.
----------------------------------------------------------------

Recommended Posts

(Java, JavaScript, Python) Comparison of string processing
Closure 4 language comparison (Python, JavaScript, Java, C ++)
python string comparison / use'list'and'in' instead of'==' and'or'
Python string processing illustration
Various processing of Python
Python, Java, C ++ speed comparison
Comparison of 4 Python web frameworks
Post processing of python (NG)
About Python string comparison operators
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Java and Python basic grammar comparison
Speed comparison of Python XML parsing
python string processing map and lambda
Speed comparison of Wiktionary full text processing with F # and Python
Python string
Basics of binarized image processing with Python
Conversion of string <-> date (date, datetime) in Python
Express Python yield in JavaScript or Java
Comparison of Japanese conversion module in Python3
Grayscale by matrix-Reinventor of Python image processing-
Learn Python! Comparison with Java (basic function)
Basic grammar of Python3 system (character string)
Drawing with Matrix-Reinventor of Python Image Processing-
Comparison of Python serverless frameworks-Zappa vs Chalice
The story of blackjack A processing (python)
Comparison of matrix transpose speeds with Python
Status of each Python processing system in 2020
Matrix Convolution Filtering-Reinventor of Python Image Processing-
A memorandum of python string deletion process
Python: String concatenation
Introduction of Python
Python string format
First Python 3 ~ First comparison ~
python string slice
Performance comparison of face detector with Python + OpenCV
View the result of geometry processing in Python
[Python3] Coarse graining of numpy.ndarray Speed comparison etc.
Python file processing
Basics of Python ①
Basics of python ①
Image processing? The story of starting Python for
Calculation of match rate of character string breaks [python]
Thorough comparison of three Python morphological analysis libraries
Copy of python
Simple comparison of Python libraries that operate Excel
Python2 string type
Comparison of R and Python writing (Euclidean algorithm)
Python string format
Python # string type
Addition of fixed processing when starting python interpreter
[Python] Chapter 02-05 Basics of Python programs (string operations / methods)
Python string inversion
Comparison of Python and Ruby (Environment / Grammar / Literal)
Introduction of Python
[Language processing 100 knocks 2020] Summary of answer examples by Python
Implementation example of simple LISP processing system (Python version)
Full-width and half-width processing of CSV data in Python
How to measure processing time in Python or Java
Cut a part of the string using a Python slice
Store Japanese (multibyte character string) in sqlite3 of python
Python implementation comparison of multi-index moving averages (DEMA, TEMA)