기타 [.NET] (C#,VB) Array to String Conversion
페이지 정보
본문
C# |
int[] myArray = new int[5] { 10, 15, 20, 25, 30 }; string myString = "Hello World!"; string input = "Coding is fun!"; char[] delimiter = new char[] { ' ' }; string[] output = input.Split(delimiter); // Output: ["Coding", "is", "fun!"] using System.Text.RegularExpressions; string input = "C#;Java,Python"; string pattern = @"[,;]+"; string[] output = Regex.Split(input, pattern); // Output: ["C#", "Java", "Python"] string[] words = new string[] { "C#", "is", "awesome!" }; string separator = " "; string result = String.Join(separator, words); // Result: "C# is awesome!" using System.Linq; int[] myArray = new int[] { 1, 2, 3, 4, 5 }; string result = string.Concat(myArray.Select(x => x.ToString())); // Result: "12345" string[] input = new string[] { "C#", "Java", "Python", "JavaScript", "Ruby" }; string result = string.Join(", ", input.Where(x => x.Length <= 4)); // Result: "C#, Java, Ruby" byte[] byteArray = new byte[] { 72, 101, 108, 108, 111 }; string result = BitConverter.ToString(byteArray); // Result: "48-65-6C-6C-6F" string[] input = new string[] { "apple", "banana", "cherry" }; string result = string.Concat(input.Select((x, i) => x.ToUpper() + (i < input.Length - 1 ? ", " : "."))); // Result: "APPLE, BANANA, CHERRY." public class MyClass { public int Id; public string Name; public override string ToString() { return $"Id: {Id}, Name: {Name}"; } } string[] planets = new string[] { "Mercury", "Venus", "Earth" }; Array.Resize(ref planets, planets.Length + 1); planets[planets.Length - 1] = "Mars"; // Planets: ["Mercury", "Venus", "Earth", "Mars"] int[] numbers = new int[] { 1, 2, 3, 4, 5 }; int[] evenNumbers = numbers.Where(x => x % 2 == 0).ToArray(); // Even numbers: [2, 4] string[] names = new string[] { "Alice", "Bob", "Eve", "David", "Carol" }; Array.Sort(names); // Sorted names: ["Alice", "Bob", "Carol", "David", "Eve"] int[] numbers = new int[] { 1, 3, 5, 2, 4 }; int firstEvenNumber = Array.Find(numbers, x => x % 2 == 0); // First even number: 2 string[] fruits = new string[] { "apple", "banana" }; Array.Resize(ref fruits, fruits.Length + 1); fruits[fruits.Length - 1] = "cherry"; // Fruits: ["apple", "banana", "cherry"] DateTime dt = new DateTime(2021, 12, 31, 18, 30, 0); string result_short = dt.ToString("d"); // Result: "12/31/2021" string result_na = dt.ToString("F"); // Result: "Friday, December 31, 2021 6:30:00 PM" |
VB |
Dim myArray As Integer() = New Integer(4) {10, 15, 20, 25, 30} Dim myString As String = "Hello World!" Dim input As String = "Coding is fun!" Dim delimiter As Char() = New Char() {" "c} Dim output As String() = input.Split(delimiter) '// Output: ["Coding", "is", "fun!"] Using System.Text.RegularExpressions End Using Dim input As String = "C#;Java,Python" Dim pattern As String = "[,;]+" Dim output As String() = Regex.Split(input, pattern) '// Output: ["C#", "Java", "Python"] Dim words As String() = New String() {"C#", "is", "awesome!"} Dim separator As String = " " Dim result As String = String.Join(separator, words) '// Result: "C# is awesome!" Using System.Linq End Using Dim myArray As Integer() = New Integer() {1, 2, 3, 4, 5} Dim result As String = String.Concat(myArray.[Select](Function(x) x.ToString())) '// Result: "12345" Dim input As String() = New String() {"C#", "Java", "Python", "JavaScript", "Ruby"} Dim result As String = String.Join(", ", input.Where(Function(x) x.Length <= 4)) '// Result: "C#, Java, Ruby" Dim byteArray As Byte() = New Byte() {72, 101, 108, 108, 111} Dim result As String = BitConverter.ToString(byteArray) '// Result: "48-65-6C-6C-6F" Dim input As String() = New String() {"apple", "banana", "cherry"} Dim result As String = String.Concat(input.[Select](Function(x, i) x.ToUpper() & (If(i < input.Length - 1, ", ", ".")))) '// Result: "APPLE, BANANA, CHERRY." Public Class [MyClass] Public Id As Integer Public Name As String Public Overrides Function ToString() As String Return $"Id: {Id}, Name: {Name}" End Function End Class Dim planets As String() = New String() {"Mercury", "Venus", "Earth"} Array.Resize(planets, planets.Length + 1) planets(planets.Length - 1) = "Mars" '// Planets: ["Mercury", "Venus", "Earth", "Mars"] Dim numbers As Integer() = New Integer() {1, 2, 3, 4, 5} Dim evenNumbers As Integer() = numbers.Where(Function(x) x Mod 2 = 0).ToArray() '// Even numbers: [2, 4] Dim names As String() = New String() {"Alice", "Bob", "Eve", "David", "Carol"} Array.Sort(names) '// Sorted names: ["Alice", "Bob", "Carol", "David", "Eve"] Dim numbers As Integer() = New Integer() {1, 3, 5, 2, 4} Dim firstEvenNumber As Integer = Array.Find(numbers, Function(x) x Mod 2 = 0) '// First even number: 2 Dim fruits As String() = New String() {"apple", "banana"} Array.Resize(fruits, fruits.Length + 1) fruits(fruits.Length - 1) = "cherry" '// Fruits: ["apple", "banana", "cherry"] Dim dt As DateTime = New DateTime(2021, 12, 31, 18, 30, 0) Dim result_short As String = dt.ToString("d") '// Result: "12/31/2021" Dim result_na As String = dt.ToString("F") '// Result: "Friday, December 31, 2021 6:30:00 PM" |
- 이전글[vb.net] LINQ(Language-Integrated Query)를 이용한 전자회로 저항갑 병렬, 직렬 계산하는 방법 23.11.20
- 다음글Get the file name from the full path in VB.Net 23.09.06
댓글목록
등록된 댓글이 없습니다.