📌  相关文章
📜  将所有数字移到给定字符串的开头

📅  最后修改于: 2021-04-18 02:21:07             🧑  作者: Mango

给定一个字符串S,任务是把所有的数字出现在字符串中,该字符串的开头。

例子:

方法:想法是遍历字符串并维护两个字符串,一个字符串包含数字,另一个字符串包含非数字字符。最后,将两个字符串按要求的顺序附加到结果中。

下面是上述方法的实现:

Java
// Java program for the above approach
 
class GFG {
 
    // Function to move all the digit
    // to the beginning of the string
    static void moveAllDigitAtBeginning(
        String str)
    {
        // Calculate the string length
        int len = str.length();
 
        // Stores the digits
        StringBuilder digits
            = new StringBuilder();
 
        // Stores the non-numeric character
        StringBuilder nonNumericCharacter
            = new StringBuilder();
 
        // Traverse the string and
        // check if there is a digit
        for (char c : str.toCharArray()) {
 
            // If character is a digit,
            // add it to the string digits
            if (c >= 48 && c <= 57) {
                digits.append(c);
            }
 
            // Otherwise, add it to the
            // string nonNumericCharacter
            else {
                nonNumericCharacter.append(c);
            }
        }
 
        // Append both the strings
        digits.append(
            nonNumericCharacter.toString());
 
        // Print the string
        System.out.print(
            digits.toString() + " ");
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given String str
        String str
            = "GeeksforGeeks123";
        moveAllDigitAtBeginning(str);
    }
}


Java
// Java program for the above approach
 
class GFG {
 
    // Function to move all the digit
    // at the beginning of the string
    static void moveAllDigitAtBeginning(
        String str)
    {
        // Replace all the non-numeric
        // characters with " " and
        // replace all digits with " "
        String moveAllDigit = str.replaceAll("\\D+", "")
                              + str.replaceAll("\\d+", "");
 
        // Print the string
        System.out.println(moveAllDigit);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given String str
        String str
            = "GeeksforGeeks1234";
        moveAllDigitAtBeginning(str);
    }
}


C#
// C# program for the above approach
using System;
using System.Text.RegularExpressions;
public class GFG
{
 
    // Function to move all the digit
    // at the beginning of the string
    static void moveAllDigitAtBeginning(
        String str)
    {
       
        // Replace all the non-numeric
        // characters with " " and
        // replace all digits with " "
        String moveAllDigit = Regex.Replace(str, "\\D+", "")
            +Regex.Replace(str, "\\d", "");
       
        // Print the string
        Console.WriteLine(moveAllDigit);
    }
 
    // Driver Code
    public static void Main(String []args)
    {
       
        // Given String str
        String str
            = "GeeksforGeeks1234";
        moveAllDigitAtBeginning(str);
    }
}
 
// This code is contributed by 29AjayKumar


输出:
123GeeksforGeeks

时间复杂度: O(N)
辅助空间: O(1)

基于正则表达式的方法:给定问题也可以使用正则表达式解决,将所有非数字字符替换为可为您提供所有数字的空字符串(“”) ,并将所有数字替换为可为您提供的空字符串(“”)所有非数字字符。最后,连接字符串并打印结果。

下面是上述方法的实现:

Java

// Java program for the above approach
 
class GFG {
 
    // Function to move all the digit
    // at the beginning of the string
    static void moveAllDigitAtBeginning(
        String str)
    {
        // Replace all the non-numeric
        // characters with " " and
        // replace all digits with " "
        String moveAllDigit = str.replaceAll("\\D+", "")
                              + str.replaceAll("\\d+", "");
 
        // Print the string
        System.out.println(moveAllDigit);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given String str
        String str
            = "GeeksforGeeks1234";
        moveAllDigitAtBeginning(str);
    }
}

C#

// C# program for the above approach
using System;
using System.Text.RegularExpressions;
public class GFG
{
 
    // Function to move all the digit
    // at the beginning of the string
    static void moveAllDigitAtBeginning(
        String str)
    {
       
        // Replace all the non-numeric
        // characters with " " and
        // replace all digits with " "
        String moveAllDigit = Regex.Replace(str, "\\D+", "")
            +Regex.Replace(str, "\\d", "");
       
        // Print the string
        Console.WriteLine(moveAllDigit);
    }
 
    // Driver Code
    public static void Main(String []args)
    {
       
        // Given String str
        String str
            = "GeeksforGeeks1234";
        moveAllDigitAtBeginning(str);
    }
}
 
// This code is contributed by 29AjayKumar
输出:
1234GeeksforGeeks

时间复杂度: O(N)
辅助空间: O(1)