在字符串之间添加字符

时间:2019-05-21 14:20:49

标签: dart flutter

我想要一个将字符串作为参数并在每3个字母后在字符串之间添加任何字符的函数。

例如:

1

将返回:

func("11111111"){}

4 个答案:

答案 0 :(得分:3)

如果我正确理解了您的问题

adb install path_to_apk
adb shell am start -a mainActivityAPP

UPD: (用于将符号从头到尾而不是从头开始)

adb shell am instrument -w <test_package_name>/<runner_class>

答案 1 :(得分:2)

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Qbert</title>
        <style>
            body { margin: 0; }
            canvas { width: 100%; height: 100%; display: block; }
        </style>
    </head>
    <body>
        <script src="js/three.js"></script>
        <script>


            var scene = new THREE.Scene();
            scene.background = new THREE.Color(0x00000);

            //KAMERA   
            var camera = new THREE.OrthographicCamera( window.innerWidth / -2, window.innerWidth /2, window.innerHeight/2,window.innerHeight/-2, -1000, 2000 );


            camera.position.x = 1;
            camera.position.y = 1;
            camera.position.z = 1;
            camera.lookAt(new THREE.Vector3(0,0,0));

            //BELEUCHTUNG
            var ambientLight = new THREE.AmbientLight(0x222222);
            scene.add(ambientLight);

            var light = new THREE.DirectionalLight( 0xffffff,0.9);
            light.position.set( -1, 10, 1 );

            scene.add( light );


            var row = 7;
            var cubesize = Math.floor((window.innerHeight)/(row*2));
            var colors = [0x4286f4,0x549652,0x00ff00];

            var game = new THREE.Group();
            var cubes = new THREE.Group();

            for(var i = 0;i<rows; i++){

                for(var j=0;j<=i;j++){

                    var color = colors[0];
                    if(i==0)color = colors[1];
                    var cubeGeo = new THREE.CubeGeometry(cubesize,cubesize.cubesize);
                    var cubeMat = new THREE.MeshLambertMaterial({color:color});
                    var cube = new THREE.Mesh(cubeGeo,cubeMat);
                    cube.position.set(j*cubesize,(rows-1)*cubesize-i*cubesize,cubesize*i-cubesize*j);
                    cube.add(cube);
                }
            }
            game.add(cubes);

            var geometry = new THREE.SphereGeometry(cubesize/2,64,64);
            var material = new THREE.MeshLambertMaterial({color:0xf4424});
            var sphere = new THREE.Mesh(geometry,material);
            sphere.position.set(0,(rows*cubesize),0);
            game.add(sphere);

            scene.add(game);






             document.addEventListener("keydown", onDocumentKeyDown, false);
                function onDocumentKeyDown(event) {


                    var keyCode = event.which;
                    if (keyCode == 81) {

                           sphere.position.x -= cubesize;
                           sphere.position.y += cubesize;

                    } else if (keyCode == 87) {

                           sphere.position.z -= cubesize;
                           sphere.position.y += cubesize;

                    } else if (keyCode == 65) {

                           sphere.position.z += cubesize;
                           sphere.position.y -= cubesize;


                    } else if (keyCode == 83) {

                           sphere.position.x += cubesize;
                           sphere.position.y -= cubesize;


                    }

                };

            var raycaster = new THREE.Raycaster();

            var pos = sphere.position;
            var direction = new THREE.Vector3(0,-1.0,0.0);

            raycaster.set(pos,direction);

            var intersects = raycaster.intersectObjects(cubes.children);

            if(intersects.length>0){

                var isec_ob = intersects[0].object;
                var material = new THREE.MeshLambertMaterial({color:colors[1]});
                isec_ob.material = material;
            }
            else{
                sphere.position.set(0,(rows*cubesize),0);
            }

            var cubesize = Math.floor((window.innerHeight/2)/rows);
            game.position.y -= Math.floor(rows/2)*cubesize/2;


            function render() {
                requestAnimationFrame( render );

                // wieder Einkommentieren, sobald Kamera definiert!
                renderer.render( scene, camera );
            }
            render();

        </script>
    </body>
</html>

答案 2 :(得分:2)

尝试一下:

  String myFunction(String str, String separator) {
    String tempString = "";
    for(int i = 0; i < str.length; i++) {
      if(i % 3 == 0 && i > 0) {
        tempString = tempString + separator;
      }
      tempString = tempString + str[i];
    }
    return tempString;
  }

并使用它,例如:

Text(myFunction("111111111", ","))

答案 3 :(得分:2)

其他解决方案可解决您提出的问题,但是,如果要在数字中添加逗号(如您的示例),则需要从右至左添加逗号。 即:12345678,而您想要的是12,345,678,而不是123,456,78

String convertFun(String src, String divider) {
    StringBuilder newStr = new StringBuilder();
    int step = 3;
    for (int i = src.length(); i > 0; i -= step) {
        newStr.insert(0, src.substring( i < step ? 0 : i - step, i));
        if (i > 3) {
            newStr.insert(0, divider);
        }
    }
    return newStr.toString();
}