C#で文字列の改行を置き換える方法は?
下の文字列から改行、スペース、タブスペースを削除する必要があるとしましょう。
排除.jpg
例
文字列のReplace()拡張メソッドを使用してそれを行うことができます。
using System;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
string testString = "Hello \n\r beautiful \n\t world";
string replacedValue = testString.Replace("\n\r", "_").Replace("\n\t", "_");
Console.WriteLine(replacedValue);
Console.ReadLine();
}
}
} 出力
上記のコードの出力は
です。Hello _ beautiful _ world
例
正規表現を使用して同じ操作を実行することもできます。 Regexは、System.Text.RegularExpressions名前空間で使用できます。
using System;
using System.Text.RegularExpressions;
namespace DemoApplication {
class Program {
static void Main(string[] args) {
string testString = "Hello \n\r beautiful \n\t world";
string replacedValue = Regex.Replace(testString, @"\n\r|\n\t", "_");
Console.WriteLine(replacedValue);
Console.ReadLine();
}
}
} 出力
上記のコードの出力は
です。Hello _ beautiful _ world
-
JavaScriptで文字列からテキストを削除するにはどうすればよいですか?
サブ文字列と置換の2つの方法を使用して、Javascriptの文字列からテキストを削除できます。 部分文字列 JS文字列クラスは、特定の文字列から部分文字列を抽出するために使用できる部分文字列メソッドを提供します。これは、文字列の一方または両方の端からテキストを削除するために使用できます。 構文 str.substr(start[, length]) 例 let a = 'Hello world' console.log(a.substr(0, 5)) console.log(a.substr(6)) console.log(a.substr(4, 3)) 出力 これにより
-
Objective-C String for iPhone SDKの文字を置き換える方法は?
Objective Cの文字を置き換えるには、Objective C文字列ライブラリの組み込み関数を使用する必要があります。この関数は、文字列の出現を、置き換えたい他の文字列に置き換えます。 Objective Cで文字列を作成するには、次のように記述する必要があります- NSString *str = @"tutori@als"; これで、この文字列の文字を置き換えて新しい文字列を作成するか、同じ文字列を変更するかを選択できます。この例では、この文字列を変更して次の行に出力します。 str = [str stringByReplacingOccurrencesOfS