-
Python樹林
【Python】複数行のテキストから特定の文字列を含む行だけを抽出する方法
テキストデータ処理において、ログファイルやCSVデータなどの複数行テキストから、特定のキーワードが含まれる行のみを抜き出したい場面は多々あります。 Pythonでは、文字列の分割メソッドとリスト内包表記を組み合わせることで、この処理を非常に簡潔か... -
Python樹林
Converting Strings to Numbers in Python: Using int/float and Validation Logic
Data obtained from file imports or user inputs is initially treated as "strings (str)". To use this data for calculations, it must be converted to the appropriate numeric type (int or float). In this article, I will explain the basic con... -
Python樹林
Pythonで文字列を数値に変換する:int/float関数の使い方と変換可否の判定ロジック
外部ファイルからの読み込みやユーザー入力で得られたデータは、最初はすべて「文字列(str)」として扱われます。これらを計算に使用するためには、適切な数値型(int または float)に変換する必要があります。 この記事では、基本的な変換方法と、変換... -
Python樹林
Aligning Strings in Python: How to Use rjust, ljust, and center
When outputting data in a table format to the console (terminal) or formatting logs for better readability, aligning text is an essential task. Python strings come with three built-in methods to adjust the position of a string within a s... -
Python樹林
Pythonで文字列を右寄せ・左寄せ・中央寄せする:rjust, ljust, centerの使い方
コンソール(ターミナル)に表形式でデータを出力したり、ログを見やすく整形したりする際、文字の配置を揃える処理は欠かせません。 Pythonの文字列型には、指定した幅の中で文字列の位置を調整するためのメソッド rjust(), ljust(), center() が用意され... -
Python樹林
Zero-Padding Numbers in Python: How to Use zfill and f-strings
When handling ID numbers, dates, or filenames (e.g., image_001.jpg), you often need to fill the missing parts with "0" to align the number of digits. This process is called "zero-padding." In Python, there are two common ways to achieve ... -
Python樹林
Pythonで数値をゼロ埋め(ゼロパディング)する:zfillメソッドとf文字列の使い分け
ID番号や日付、ファイル名(image_001.jpgなど)を扱う際、桁数を揃えるために足りない部分を「0」で埋めたい場合があります。これを「ゼロ埋め」や「ゼロパディング」と呼びます。 Pythonでは、文字列型のメソッド zfill() を使う方法と、f-string(フォ... -
Python樹林
Splitting Strings into Lists in Python: Usage of split() and Whitespace Handling
When parsing CSV data or breaking down sentences into words, you often need to split a single long string into multiple parts according to specific rules. Python's string type provides the standard split() method for this purpose. In thi... -
Python樹林
Pythonで文字列をリストに分割する:split()メソッドの使い方と空白処理の注意点
CSVデータの解析や、文章を単語ごとに分解する処理など、1つの長い文字列を特定のルールに従って複数のパーツに分割したい場面は頻繁にあります。 Pythonの文字列型には、このための標準メソッドとして split() が用意されています。 この記事では、split(... -
Python樹林
Validating Strings in Python: How to Use isalnum, isalpha, and isdecimal
When processing user input, it is often necessary to perform validation checks, such as asking, "Is the input value only numbers?" or "Does it consist only of alphabets?" Python's string type (str) provides a set of convenient methods th... -
Python樹林
Pythonで文字列の種類を判定する:isalnum, isalpha, isdecimalなどの活用法
ユーザーからの入力を処理する際、「入力された値は数字だけか?」「アルファベットだけで構成されているか?」といったチェック(バリデーション)が必要になることはよくあります。 Pythonの文字列型(str)には、文字列の内容を検査して True または Fa... -
Python樹林
Converting Uppercase and Lowercase Strings in Python: Using upper, lower, capitalize, and title
Converting uppercase and lowercase letters is a common task, often used to standardize inconsistent user inputs for email addresses or to format article titles. Python's string type (str) provides several useful methods for performing th... -
Python樹林
Pythonで文字列の大文字・小文字を変換する:upper, lower, capitalize, titleの使い方
ユーザーが入力したメールアドレスの表記ゆれを統一したり、記事のタイトルを見やすく整形したりするために、文字列の大文字・小文字を変換する処理は頻繁に行われます。 Pythonの文字列型(str)には、このような変換を行うための便利なメソッドがいくつ... -
Python樹林
Removing Whitespace in Python Strings: Using strip, lstrip, and rstrip
Text data from user input or files often contains unintended "whitespace" or "newline codes" at the beginning or end. If these are left remaining, they can cause errors when comparing strings or saving data to a database. Python provides... -
Python樹林
Pythonで文字列の空白削除:strip, lstrip, rstripの使い方と注意点
ユーザーからの入力データやファイルから読み込んだテキスト行には、意図しない「空白(スペース)」や「改行コード」が前後についていることがよくあります。これらが残っていると、文字列の比較やデータベースへの保存時に不具合の原因となります。 Pyth... -
C#樹林
Implementing Event Notifications with IObserver and IObservable in C#
In .NET Framework and .NET Core, System.IObservable<T> and System.IObserver<T> are the standard interfaces provided for implementing the Observer Pattern. By using these interfaces, you can build a push-based notification sys... -
C#樹林
【C#】IObserverとIObservableを用いたイベント通知の実装
.NET Frameworkおよび.NET Core以降では、オブザーバーパターン(Observer Pattern)を実装するための標準インターフェースとして、System.IObservable<T> と System.IObserver<T> が用意されています。これらを利用することで、データ発行元... -
C#樹林
[C#] Implementing the IDisposable Interface to Properly Release Resources
While the .NET Garbage Collector (GC) automates memory management, it does not manage "unmanaged resources" such as file handles, database connections, or network sockets. To properly release these resources, a class must implement the I... -
C#樹林
【C#】IDisposableインターフェイスを実装してリソースを適切に解放する
.NETのガベージコレクタ(GC)はメモリ管理を自動化しますが、ファイルハンドル、データベース接続、ネットワークソケットといった「アンマネージドリソース」までは管理しません。これらのリソースを適切に解放するためには、クラスにIDisposableインター... -
C#樹林
[C#] Sorting Collections with Custom Rules by Implementing IComparer
When using the List<T>.Sort() method, there are cases where you want to sort based on a specific property or a special calculation formula, rather than the default order (e.g., ascending order). Implementing IComparable<T> on... -
C#樹林
【C#】IComparer
を実装して独自のルールでコレクションをソートする List<T>.Sort()メソッドを使用する際、デフォルトの並び順(昇順など)ではなく、「特定のプロパティに基づいて並び替えたい」「特殊な計算式で順序を決めたい」という場合があります。 クラス自体にIComparable<T>を実装するとそのクラスの「... -
C#樹林
[C#] Implementing IComparable for Custom Class Comparison and Sorting
C#'s List<T>.Sort() and Array.Sort() methods work out-of-the-box for basic types like numbers and strings. However, if you call them on a custom class or struct, an exception (InvalidOperationException) occurs because the program d... -
C#樹林
【C#】IComparable
を実装して自作クラスの大小比較とソートを行う C#のList<T>.Sort()メソッドやArray.Sort()メソッドは、数値や文字列などの基本的な型であればそのまま機能しますが、自作のクラスや構造体に対して呼び出すと、どのように順序を付ければよいか分からず例外(InvalidOperationException)が発生しま... -
C#樹林
[C#] Implementing Equality Logic Outside Classes with IEqualityComparer
Usually, equality determination for a class (whether two objects are the same) is defined by implementing IEquatable<T> within the class itself. However, this approach may not be suitable in the following cases: When you want to ch... -
C#樹林
【C#】IEqualityComparer
でクラスの外部に等価判定ロジックを持たせる 通常、クラスの等価判定(2つのオブジェクトが同じかどうか)は、そのクラス自身にIEquatable<T>を実装して定義します。しかし、以下のようなケースではそれが適さない場合があります。 ソースコードを変更できないクラス(外部ライブラリなど)の比... -
C#樹林
[C#] Defining Equality for Custom Classes by Implementing IEquatable
In C#, when handling custom classes (reference types) in collections like List or HashSet, you may find that they are not determined to be "equal" as intended. This is because the default comparison behavior of classes is based on "refer... -
C#樹林
【C#】IEquatable
を実装して自作クラスの等価判定を定義する C#において、自作したクラス(参照型)をコレクション(ListやHashSetなど)で扱う際、意図した通りに「等しい」と判定されないことがあります。これは、クラスのデフォルトの比較動作が「値(プロパティ)の一致」ではなく「参照(メモリアドレス)の一致... -
C#樹林
[C#] Implementing Iterator Methods Returning IEnumerable Using yield return
In C#, when implementing a method that returns a collection or sequence (a series of data), it is often recommended to use the yield return syntax instead of creating and returning a collection like List<T>. Using yield allows the ... -
C#樹林
【C#】yield returnを使ってIEnumerable
を返すイテレータメソッドを実装する C#において、コレクションやシーケンス(データの並び)を返すメソッドを実装する場合、List<T>などのコレクションを生成して返す代わりに、yield return構文を使用することが推奨されるケースが多くあります。 yieldを使用すると、コンパイラが自動... -
未分類
[C#] Technique to Override Default Interface Implementations and Reuse Original Logic
The "Default Interface Methods" feature introduced in C# 8.0 made it possible for interfaces themselves to hold logic. Normally, if you define the same method in the implementing class, the default implementation in the interface is comp...