Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2024/25/2
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

String operations

See also:

Strip

(1) strip()
def main():
    s = "   aa   "
    print("'{0}'".format(s.strip()))  # 'aa'
    print("'{0}'".format(s.lstrip()))  # 'aa   '
    print("'{0}'".format(s.rstrip()))  # '   aa'
import std.stdio;
import std.string;  // from here

void main()
{
    string s = "   aa   ";
    writefln("'%s'", s.strip()); // 'aa'
    writefln("'%s'", s.stripLeft()); // 'aa   '
    writefln("'%s'", s.stripRight()); // '   aa'
}

Replace

(2) replace()
def main():
    s = "cat dog cat"
    print(s.replace("cat", "kitten"))
import std.stdio;
import std.array; // replace()

void main()
{
    string s = "cat dog cat";
    writeln(s.replace("cat", "kitten"));
}

Output:

kitten dog kitten

startsWith, endsWith

(3) startsWith(), endsWith()
def main():
    url = "https://dlang.org"
    print(url.startswith("https"))  # True
    print(url.endswith(".com"))  # False
import std.stdio;
import std.algorithm.searching;  // from here

void main()
{
    string url = "https://dlang.org";
    writeln(url.startsWith("https")); // true
    writeln(url.endsWith(".com")); // false
}

indexOf

(4) indexOf()
def main():
    s = "joker"

    print(s.find("k"))  # 2
    print(s.find("ke"))  # 2
    print(s.find("xxx"))  # -1

    idx = s.find("ke")  # 2
import std.stdio;
import std.string;  // from here

void main()
{
    string s = "joker";

    writeln(s.indexOf("k")); // 2
    writeln(s.indexOf("ke")); // 2
    writeln(s.indexOf("xxx")); // -1

    int idx = cast(int) s.indexOf("ke"); // convert to int
    writeln(idx); // 2
}

The return type of indexOf() is actually ptrdiff_t .

There's also lastIndexOf() that searches for the last occurrence of a character/substring in a string.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 June 28, 20:24